说明:蓝色=命令名称
浅绿=命令参数
浅蓝=选项
紫色=目录
系统环境:CentOS 5.5 x86_64
python版本:Python 2.7.3
今天翻看以前的博客发下一个有趣的问题(http://linux521.blog.51cto.com/4099846/826779)
要求0-9 十个数的排列组合
例如:
6758912034
1203467589
8956712034
1203489567
6759812034
#!/usr/bin/env python #-*- coding:utf-8 -*- #Author:left_left def random(a, tt, b): len_a = len(a) t = tt - len_a j = 0 if len_a > 1: while j < len_a -1: j += 1 b[t] = a[0] random(a[1:], tt, b) a[0],a[j] = a[j],a[0] else: print "%s%s%s%s%s%s%s%s%s%s"% tuple(b) m = range(11) random(m, len(m), m[1:])
比第一次简洁很多,忘各位大牛指点。
c版
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char string[]="0123456789#"; char *result; result = malloc(strlen(string)-1); random(string, strlen(string), result); return 0; } void random(char *s, int s_len, char *r){ int l_s_len = strlen(s); int i = 1; char l_s[l_s_len]; char c; strcpy(l_s, s); while(i < l_s_len){ r[s_len - l_s_len] = l_s[0]; random(&l_s[1], s_len, r); c = l_s[0]; l_s[0] = l_s[i]; l_s[i] = c; i++; } if(l_s_len==1){ printf("%s\n", r); } }
效率相当高:
[root@left-left ~]# time ./random_char >/dev/null real 0m0.467s user 0m0.463s sys 0m0.003s
c优化版
#include <stdio.h> #include <stdlib.h> #include <string.h> int s_len; char *result; int main(){ char string[]="0123456789#"; s_len = strlen(string); result = malloc(strlen(string)-1); randomChar(string, strlen(string)); return 0; } int randomChar(char *s, int l_s_len){ int i = 1; char c; if(l_s_len==1){ printf("%s\n", result); return 0; } while(i < l_s_len){ result[s_len - l_s_len] = s[0]; randomChar(&s[1], l_s_len-1); c = s[0]; s[0] = s[i]; s[i] = c; i++; } for(i=0;i<l_s_len-2;i++){ s[i]=s[i+1]; } s[i] = c; return 0; }
运行时间
[root@left-left ~]# gcc -O2 -o randomchar randomchar.c [root@left-left ~]# time ./randomchar >/dev/null real 0m0.141s user 0m0.141s sys 0m0.000s
c优化版2
#include <stdio.h> #include <stdlib.h> #include <string.h> int s_len; int s_len_1; int s_len_2; char *result; char string[]="0123456789#"; int main(){ s_len = strlen(string); s_len_1 = s_len - 1; s_len_2 = s_len - 2; result = malloc(strlen(string)-1); randomChar(0); return 0; } int randomChar(int l_s_len){ int i = l_s_len + 1; char c; if(l_s_len==s_len_1){ printf("%s\n", result); return 0; } while(i < s_len){ result[l_s_len] = string[l_s_len]; randomChar(l_s_len + 1); c = string[l_s_len]; string[l_s_len] = string[i]; string[i] = c; i++; } for(l_s_len;l_s_len<s_len_2;l_s_len++){ string[l_s_len]=string[l_s_len+1]; } string[l_s_len] = c; return 0; }
运行时间
[root@left-left ~]# gcc -O2 -o randomchar randomchar.c [root@left-left ~]# time ./randomchar >/dev/null real 0m0.137s user 0m0.133s sys 0m0.004s
c 多进程版
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> int s_len; int s_len_1; int s_len_2; char *result; char string[]="0123456789"; FILE *fp; int main(){ int i; char c; char s[]="\n"; char f[10]; s_len = strlen(string); s_len_1 = s_len - 1; s_len_2 = s_len - 2; result = malloc(s_len+1); result[s_len]=s[0]; for(i=0; i<s_len;i++){ result[s_len_1] = string[s_len_1]; if(fork()==0){ sprintf(f, "tmp_%d",i); fp=fopen(f,"a"); randomChar(0); fclose(fp); exit(0); } c = string[s_len_1]; string[s_len_1] = string[i]; string[i] = c; } for(i=0;i<s_len;i++){ wait(0); } return 0; } int randomChar(int l_s_len){ int i = l_s_len + 1; char c; if(l_s_len==s_len_1){ fputs(result,fp); return 0; } while(i < s_len){ result[l_s_len] = string[l_s_len]; randomChar(l_s_len + 1); c = string[l_s_len]; string[l_s_len] = string[i]; string[i] = c; i++; } for(l_s_len;l_s_len<s_len_2;l_s_len++){ string[l_s_len]=string[l_s_len+1]; } string[l_s_len] = c; return 0; }
运行时间
[zuopucun@sys-test-czx ~]$ time ./randomchar real 0m0.050s user 0m0.183s sys 0m0.003s