Problem N
Time Limit : 1000/1000ms (Java/Other) Memory Limit :32768/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
Problem Description
Give you a number on baseten,you should output it on base two.(0 < n < 1000)
Input
For each case there is apostive number n on base ten, end of file.
Output
For each case output a numberon base two.
Sample Input
1
2
3
Sample Output
1
10
11
算法分析:
题意就是十进制转换二进制,递归做法
代码实现:
#include <bits/stdc++.h>
using namespace std;
char a[17]="0123456789ABCDEF";
void conversion(int n,int k)
{int r;
r=n%k;
n=n/k;
if(n!=0) conversion(n,k);
cout<<a[r];
}
int main()
{
int n;
while(cin>>n)
{conversion(n,2);//这里可以扩展,可以把2改为8,16,都可,实现向八进制十六进制转化
cout<<endl;
}
return 0;
}