1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include<stdio.h> #include<stdlib.h> #include<string.h> void tower(int n, int source[], int dest[], int auxiliary[],int m[]); int source[1],dest[1],auxiliary[1]; int step[32]= {0}; int subtract(int n1[],int n2[]) { int i,n3[32]= {0},flag=1,j; for(i=31; i>0; i--) { n3[i]=n1[i]-n2[i]; n3[i-1]=n3[i-1]+(n3[i])/10-1; n3[i]=(n3[i])%10; } n3[0]=n1[0]-n2[0]; for(i=0; i<=31; i++) { if(n3[i]!=0) { flag=0; break; } } //printf("%d !",flag); return flag; } void add(int s[]) { int l,i; s[31]+=1; for(i=31; i>0; i--) { if(s[i]>=10) { s[i-1]+=s[i]/10; s[i]-=10; } } } int main() { int n,i,j,k,l; char temp[32]= {0}; int real[32]= {0}; while(1) { for(j=0; j<32; j++) { temp[j]=0; real[j]=0; step[j]=0; } scanf("%d%s",&i,temp); if(i==0&&temp[0]=='0') break; l=strlen(temp); source[0]=i; dest[0]=0; auxiliary[0]=0; for(j=31,k=l-1; k>=0; j--,k--) { real[j]=temp[k]-'0'; } tower(i,source,dest,auxiliary,real); if(i%2==1) printf("%d %d %d\n",source[0],dest[0],auxiliary[0]); else printf("%d %d %d\n",source[0],auxiliary[0],dest[0]); } return 0; } void tower(int n, int source[], int dest[], int auxiliary[],int m[]) { if(n==0) { return ; } else { tower(n-1,source,auxiliary,dest,m); if(subtract(m,step)) return ; add(step); source[0]--; dest[0]++; if(subtract(m,step)) return ; tower(n-1,auxiliary,dest,source,m); } return ; } |
Direct link: https://paste.plurk.com/show/399701