7-6 整数分解为若干项之和 (15 分)

#include <iostream> using namespace std; const int N = 40; int q[N]; int k, n; void dfs(int st, int cnt, int total) { if(total == n) { k++; cout << n << "="; cout << q[0]; for(int i = 1; i < cnt; i++) cout << "+" << q[i]; if((k % 4 == 0)) cout << endl; else if(st != n) cout << ";"; return; } if(total > n) return; if(total < n) { for(int i = st; i <= n; i++) { q[cnt] = i; dfs(i, cnt+1, total + i); } } } int main() { cin >> n; dfs(1, 0, 0); return 0; }

浙公网安备 33010602011771号