HDU_2051——十进制到二进制转换

Problem Description
Give you a number on base ten,you should output it on base two.(0 < n < 1000)
 

 

Input
For each case there is a postive number n on base ten, end of file.
 

 

Output
For each case output a number on base two.
 

 

Sample Input
1 2 3
 

 

Sample Output
1 10 11
 1 #include <cstdio>
 2 int main()
 3 {
 4     int n, i, ans[11];
 5     while(~scanf("%d",&n))
 6         {
 7             if(n==0)
 8                 {
 9                     printf("0\n");    
10                 }
11             for(i=0;n;i++)
12                 {
13                     ans[i]=n%2;
14                     n=n/2;
15                 }
16             for(i--;i>=0;i--)
17                 {
18                     printf("%d",ans[i]);    
19                 }
20             printf("\n");
21         }
22     return 0;    
23 }

 

posted @ 2013-07-11 22:17  瓶哥  Views(313)  Comments(0Edit  收藏  举报