题目描述:
一个整数可以由连续的自然数之和来表示给定一个整数,计算该整数有几种连续自然数之和的表达式,且打印出每种表达式
示例:
输入
9
输出
9=9
9=4+5
9=2+3+4
Result:3
C++源码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// 函数用于找到所有和为target的连续整数序列
vector<vector<int>> findAllSequences(int target) {
vector<vector<int>> sequences;
for (int start = 1; start < target; ++start) {
int sum = 0;
vector<int> sequence;
for (int i = start; i <= target; ++i) {
sum += i;
sequence.push_back(i);
if (sum == target) {
sequences.push_back(sequence);
break;
}
else if (sum > target) {
brea