Test series: input a positive integer n, and then output the binary number corresponding to n-------Recursive algorithm is required to implement

#include <iostream>
using namespace std;
void dec2bin(int n) {
    
    
	int m = n;
	if (m == 0) cout << m; 
	else {
    
    
		dec2bin(n / 2);
		cout << n % 2;
		return;
	} 
}
int main() {
    
    
	int n;
	cout << "请输入一个整数:";
	cin >> n;
	cout << n << "对应的二进制形式为:";
	dec2bin(n);
	cout << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43216714/article/details/127964825