题目:
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
题意:就是把一个十进制数转换为二进制数。。
思路:这题我用了递归调用,因为十进制转换为二进制是一直除以二取余数,而先求出来的余数要放到最后,所以用递归调用可以实现这个,看代码。。
代码:
#include <iostream>
using namespace std;
int f(int n)
{
int i;
if(n==0)
return 0;
else if(n==1)
{
cout<<n;
return 0;
}
else
{
i=n%2;
n=n/2;
f(n);
cout<<i;
return 0;
}
}
int main()
{
int i,n;
while(cin>>n)
{
f(n);
cout<<endl;
}
return 0;
}