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
简单题意:
给出你一个十进制的数字,然后转化成二进制,输出。
解题思路形成过程:
这个题怎么这么简单?第一反应就是这样。我原本以为C++ 类库中有直接转化成二进制的函数,像setf(ios::oct)就是以八进制输出一个数。但是没有。所以转换到动态规划中,写一个简单的递归即可。
感想:
动态规划、递归解决问题的思想是十分重要的。
AC代码:
#include <iostream>
using namespace std;
void f(int n)
{
if(n/2) f(n/2);
cout<<n%2;
}
int main()
{
int n;
while(cin>>n)
{
f(n);
cout<<endl;
}
return 0;
}