說明:藍色=命令名稱python
淺綠=命令參數linux
淺藍=選項bash
紫色=目錄
dom
系統環境:CentOS 5.5 x86_64ide
python版本:Python 2.7.3優化
今天翻看之前的博客發下一個有趣的問題(http://linux521.blog.51cto.com/4099846/826779)spa
要求0-9 十個數的排列組合blog
例如:進程
6758912034
1203467589
8956712034
1203489567
6759812034utf-8
#!/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