标签:
1 #include <stdio.h> 2 void toBinary(int n,int x); 3 int main() 4 { 5 int n; 6 freopen("out1.txt","w",stdout); 7 n=1; 8 while(n<=255) 9 { 10 //scanf("%d",&n); 11 printf("%d---->",n); 12 toBinary(n,0); 13 printf("\n"); 14 n++; 15 } 16 17 return 0; 18 } 19 void toBinary(int n,int x) 20 { 21 int t; 22 if(n==0) 23 return; 24 else 25 { 26 t=n%2; 27 toBinary(n/2,x+1); 28 if(t) 29 { 30 if(x==1) 31 { 32 if(n/2==0) 33 printf("2"); 34 else printf("+2"); 35 } 36 else 37 { 38 if(n/2==0) 39 printf("2(%d)",x); 40 else printf("+2(%d)",x); 41 } 42 } 43 } 44 }
1 #include <stdio.h> 2 void toBinary(int n,int x); 3 int main() 4 { 5 int n; 6 /*scanf("%d",&n); 7 toBinary(n,0);*/ 8 9 freopen("out2.txt","w",stdout); 10 n=1; 11 while(n<=255) 12 { 13 //scanf("%d",&n); 14 printf("%d---->",n); 15 toBinary(n,0); 16 printf("\n"); 17 n++; 18 } 19 20 return 0; 21 } 22 void toBinary(int n,int x) 23 { 24 int t; 25 if(n==0) 26 return; 27 else 28 { 29 t=n%2; 30 toBinary(n/2,x+1); 31 if(t) 32 { 33 if(x==1) 34 { 35 if(n/2==0) 36 printf("2"); 37 else printf("+2"); 38 } 39 else 40 { 41 if(n/2==0) 42 { 43 //printf("2(%d)",x); 44 if(x==0) printf("2(0)"); 45 else 46 { 47 printf("2(",x); 48 toBinary(x,0); 49 printf(")"); 50 } 51 } 52 else 53 { 54 //printf("+2(%d)",x); 55 if(x==0) printf("+2(0)"); 56 else 57 { 58 printf("+2(",x); 59 toBinary(x,0); 60 printf(")"); 61 } 62 } 63 } 64 } 65 } 66 }
标签:
原文地址:http://www.cnblogs.com/huashanqingzhu/p/4417467.html