编写一个递归函数,将10进制转化成二进制
输入输入一个10进制数输出输出一个二进制数
样例输入
116
样例输出
1110100
代码1:
#include <iostream> #include <string> using namespace std; string str; void f(int n) { if (n == 0) return; else { f(n / 2); str += char(n % 2 + '0'); } } int main() { int n; while (cin >> n) { str = ""; f(n); cout << str << endl; } return 0; }
代码2:
#include <iostream> #include <string> using namespace std; string f(int n) { string str; while (n) { str = char(n % 2 + '0') + str; n /= 2; } return str; } int main() { int n; while (cin >> n) { cout << f(n) << endl; } return 0; }