练习三1015


Problem O
Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 29
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
 

Statistic | Submit | Back
思路:
n除以2取余,存入数组,除到1时存入1结束。
代码:
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int i,a[11];
if(n==0)
cout<<0<<endl;
else{
for(i=0;;i++)
{
if(n==1)
{
a[i]=1;break;
}
else
{
a[i]=n%2;
n=n/2;
}
}
}
for(int j=i;j>=0;j--)
cout<<a[j];
cout<<endl;
}
return 0;
}

你可能感兴趣的:(练习三1015)