自然数的和是78,这三个数分别是多少
连续的情况:
不连续的情况如下:
情况太多就截图一部分
#include<iostream>
using namespace std;
#include <fstream>
#include <cmath>
int coun = 0;
ofstream fo("result.txt", ofstream::out);
int Result(int j)
{
for (int k = 0; k <= j; k++)
{
for (int s = k; s <= j; s++)
{
for (int i = s; i <= j; i++)
{
if (((i + s + k) == j) && (((abs(i - s) == 1) && (abs(s - k) == 1)) && (abs(i - k) == 2)) || (((abs(i - k) == 1) && (abs(s - k) == 1)) && (abs(i - s) == 2)) || (((abs(i - k) == 1) && (abs(i - k) == 1)) && (abs(k - s) == 2)))
{
//solution[count][3]={R[i],R[s],R[k]};
if (fo.is_open())
{
fo << i << "," << s << "," << k<<endl;
}
coun++;
cout << i << "," << s << "," << k << "\n";
}
}
}
}
cout << "自然数的和是" << j << "三的组合个数为:" << coun << "\n";
fo.close();
return j;
}
int main()
{
int a = 0;
cout << "请输入自然数的和:";
cin >> a;
Result(a);
system("pause");
return 0;
}
把if (((i + s + k) == j) && (((abs(i - s) == 1) && (abs(s - k) == 1)) && (abs(i - k) == 2)) || (((abs(i - k) == 1) && (abs(s - k) == 1)) && (abs(i - s) == 2)) || (((abs(i - k) == 1) && (abs(i - k) == 1)) && (abs(k - s) == 2)))
改成if (((i + s + k) == j))就是不一定连续的组合
2017-12-31 · 知道合伙人教育行家
78/3=36
36+1=37
36-1=35
答:这三个连续自然数是35、36、37
这三个数为:
25,26,27