看板 b99902HW 關於我們 聯絡資訊
如題,testgirl的第47題,無論小弟我如何嘗試, 執行時間仍然會爆表 請問強者們,要怎麼做才能減少我的運算時間? 麻煩強者賜教 底下是題目和我的程式碼 題目: Practice 04 (recursive function) Given a integer N, print out the N! combination composed of first N upper letters. The output answers should be in lexical order. Hint: you may need arrays to assist you in the recursive process. Input format: One integer per line. The integers will range from 1 to 10. Output format: Print out all the combinations, one combination per line. Between every case, you should print a line cotaining only '.' . Example: Input: 1 2 3 Output: A . AB BA . ABC ACB BAC BCA CAB CBA 然後這是我的程式碼: #include<stdio.h> void re(int l,int n,int s[],int p[]); int main() { int i,j,k,b=0; int s[10],p[10]; int n,l; while(scanf("%d",&l)!=EOF) { if(b==0) b=1; else printf(".\n"); for(i=0;i<l;i++) s[i]=i; re(l,0,s,p); } return 0; } void re(int l,int n,int s[],int p[]) { int i,j,h; for(i=0;i<l;i++) { if(s[i]==-1) continue; p[n]=s[i]; s[i]=-1; re(l,n+1,s,p); s[i]=p[n]; } if(n==l) { for(i=0;i<l;i++) printf("%c",'A'+p[i]); printf("\n"); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.242.229
suhorng:路人路過... 把 printf("%c", 'A'+p[i]); 換成 putchar( 11/20 21:42
suhorng:'A'+p[i]); 應該就可以了 :P 這題最花時間的是 output 11/20 21:42
xluds24805:l 和 1 看起來好像@@ 11/20 21:44
felixgwu:非常感謝suhorng的回答,上了寶貴的一課。 11/20 22:24
xluds24805:為什麼換成putchar就比較不會花時間丫~_~? 11/20 23:27
innocentguy:suhrong耶........... 11/21 23:17