Chinaunix
标题:
最简单最快速对数字任意分组
[打印本页]
作者:
SlowLaris
时间:
2013-12-25 23:19
标题:
最简单最快速对数字任意分组
本帖最后由 SlowLaris 于 2013-12-25 23:20 编辑
总数:64,假如分四组
结果:
01-16
17-32
33-48
49-64
总数:64,假如分二组
结果:
01-32
33-64
看看谁的算法最简便~
作者:
yestreenstars
时间:
2013-12-25 23:34
本帖最后由 yestreenstars 于 2013-12-25 23:35 编辑
[root@localhost ~]# awk -vn=2 -vs=64 'BEGIN{p=1;q=s/n;l=length(s);while(++i<=n){printf "%0"l"d-%0"l"d\n",p,q;p+=s/n;q+=s/n}}'
01-32
33-64
[root@localhost ~]# awk -vn=4 -vs=64 'BEGIN{p=1;q=s/n;l=length(s);while(++i<=n){printf "%0"l"d-%0"l"d\n",p,q;p+=s/n;q+=s/n}}'
01-16
17-32
33-48
49-64
[root@localhost ~]#
复制代码
作者:
SlowLaris
时间:
2013-12-25 23:45
回复
2#
yestreenstars
如果是奇数分组呢?如果总是是奇数呢?
作者:
yestreenstars
时间:
2013-12-25 23:47
回复
3#
SlowLaris
给个栗子~
作者:
SlowLaris
时间:
2013-12-25 23:54
回复
4#
yestreenstars
awk -vn=7 -vs=64 'BEGIN{p=1;q=s/n;l=length(s);while(++i<=n){printf "%0"l"d-%0"l"d\n",p,q;p+=s/n;q+=s/n}}'
01-09
10-18
19-27
28-36
37-45
46-54
55-63
作者:
yestreenstars
时间:
2013-12-25 23:56
回复
5#
SlowLaris
那你想要什么结果?
作者:
SlowLaris
时间:
2013-12-26 00:03
回复
6#
yestreenstars
不管分几组,所有数字都在,刚才试了下7组,但是64没了,
作者:
yestreenstars
时间:
2013-12-26 00:12
本帖最后由 yestreenstars 于 2013-12-26 00:23 编辑
回复
7#
SlowLaris
[root@localhost ~]# awk -vn=7 -vs=64 'BEGIN{t=s/n;p=1;q=t;l=length(s);while(++i<=n){if(i==n)q=s;printf "%0"l"d-%0"l"d\n",p,q;p+=t;q+=t}}'
01-09
10-18
19-27
28-36
37-45
46-54
55-64
[root@localhost ~]#
复制代码
作者:
SlowLaris
时间:
2013-12-26 00:45
回复
8#
yestreenstars
感谢,请问你是怎么学好的awk?
作者:
yestreenstars
时间:
2013-12-26 09:04
回复
9#
SlowLaris
多泡论坛,日积月累~
作者:
这个冬天不冷
时间:
2013-12-26 11:59
[root@everIover ~]# awk -vn=3 -vs=64 'BEGIN{t=int(s/n);x=s%n;for(i=1;i<s-x-t;i+=t)printf("%.2d-%d\n",i,i+t-1);printf("%s-%s\n",i,s)}'
01-21
22-42
43-64
[root@everIover ~]# awk -vn=7 -vs=64 'BEGIN{t=int(s/n);x=s%n;for(i=1;i<s-x-t;i+=t)printf("%.2d-%d\n",i,i+t-1);printf("%s-%s\n",i,s)}'
01-9
10-18
19-27
28-36
37-45
46-54
55-64
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2