标签:使用 init while string ini cout out cpp ios
基本思想:
使用迭代会很麻烦,还不如递归进行字符串拼接;
关键点:
无;
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 15;
int num[maxn];
string fun(int n) {
	if (n == 0)
		return "0";
	int index = maxn - 1;
	string s="";
	while (n != 0) {
		for (; index >= 0; index--) {
			if (num[index] <= n) {
				if (index == 1)
					s = s + "2+";
				else {
					s = s + "2(" + fun(index) + ")+";
				}
				n -= num[index];
			}
		}
	}
	s.pop_back();
	return s;
}
void init() {
	for (int i = 0; i < maxn; i++) {
		num[i] = pow(2, i);
	}
}
int main() {
	int n;
	init();
	while (cin >> n) {
		string s = fun(n);
		cout << s << endl;
	}
}
标签:使用 init while string ini cout out cpp ios
原文地址:https://www.cnblogs.com/songlinxuan/p/12493327.html