c++简单题,跪求高手指错

来源:百度知道 编辑:UC知道 时间:2024/06/14 09:33:04
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

#include<iostream>
using namespace std;
int main()
{
int a[1001],t,n,i,x;
while(cin>>n)
{
i=0;
while(n>1)
{
t=n%2;
n/=2;
i++;
a[i]=t;
x=i;
}
cout<<'1';
for(i=x;i>0;i--)
cout<<a[i];
cout<<endl;
}
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int a[12];int base10;
while(cin>>base10)
{
int dig=0;
while (base10>0)
{
a[dig++]=base10&1;
base10>>=1;
}
for (dig--;dig>=0;dig--){cout<<a[dig];}
cout<<endl;
}
return 0;
}

貌似没问题,会不会是测试系统有问题,把0也作为一个测试例子了.

#include<iostream>
using namespace std;
int main()
{
int a[1001] = {0};
int t = 0;
int n = 0;
int i = 0;
int x = 0;
while(cin>>n)
{
i=0;
while(n>1)
{
t=n%2;
n/=2;
i++;
a[i]=t;
x=i;
}
cout<<'1';
for(i=x;i>0;--i)
{
cout<<a[i];
}
cout<<endl;
}
return 0;
}

这样就没问题了
因为