枚举法,三个整数之和等于6.共有几组这样的三个数?
发布网友
发布时间:2023-09-06 21:32
我来回答
共1个回答
热心网友
时间:2023-09-15 01:03
//js 代码循环出所有答案组合
let z;
for (let x = 0; x < 7; x++) {
for (let y = 0; y < 7; y++) {
z = 6 - x - y;
if (z > -1) {
console.log(x, y, z, '枚举');
}
}
}
打印出来结果如下,去除重复即是答案。
0 0 6 "枚举"
0 1 5 "枚举"
0 2 4 "枚举"
0 3 3 "枚举"
0 4 2 "枚举"
0 5 1 "枚举"
0 6 0 "枚举"
1 0 5 "枚举"
1 1 4 "枚举"
1 2 3 "枚举"
1 3 2 "枚举"
1 4 1 "枚举"
1 5 0 "枚举"
2 0 4 "枚举"
2 1 3 "枚举"
2 2 2 "枚举"
2 3 1 "枚举"
2 4 0 "枚举"
3 0 3 "枚举"
3 1 2 "枚举"
3 2 1 "枚举"
3 3 0 "枚举"
4 0 2 "枚举"
4 1 1 "枚举"
4 2 0 "枚举"
5 0 1 "枚举"
5 1 0 "枚举"
6 0 0 "枚举"