例如 10 = 1 + 2 + 3 + 4。
代码:
#include <iostream> #include <vector> using namespace std; void func(int N) { int i = 0, tmp = 0; int a = 0; vector<int> vec; if (N % 2 == 1) { a = (N-1)/2; vec.push_back(a); vec.push_back(a+1); } else if (N%3 == 0) { a = N / 3; vec.push_back(a-1); vec.push_back(a); vec.push_back(a+1); } else if (N % 2 == 0) { for (i = 3; i <= N/2; i+=2) { if (N % i == 0) { a = N / i; if (a > i) { a = N/i - i/2; } else { a = i/2-a+1; i = (N/i)*2; } while ((a > 0) && (i > 0)) { vec.push_back(a); a++; i--; } break; } } } if (vec.size() > 0) { cout << "N="<< N << " >> " << vec[0]; for (i = 1; (size_t)i < vec.size(); i++) { cout << " + " << vec[i]; } } else { cout << N << " >> NO RESULT"; } cout << endl << endl; } void main() { int i = 0; for (i = 2; i< 100; i++) { func(i); } cin >> i; }