代码
int n = 100;
Action<int, int, Action<int>> myfor = (start, end, callback) => {
for (int i = start; i < end; i++) {
callback(i);
}
};
myfor(1, n, (a) => myfor(1, n, (b) => myfor(1, n, (c) => {
if (a * a + b * b == c * c)
Console.WriteLine("a:{0},b:{1},c:{2},n:{3}", a, b, c, n);
})));
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
for (int c = 1; c < n; c++) {
if (a * a + b * b == c * c)
Console.WriteLine("a:{0},b:{1},c:{2},n:{3}", a, b, c, n);
}
}
}
var r = from a in Enumerable.Range(1, n)
from b in Enumerable.Range(a, n)
from c in Enumerable.Range(b, n)
where a + b + c < n &&
a * a + b * b == c * c
select new { A = a, B = b, C = c };
foreach (var item in r) {
Console.WriteLine("a:{0},b:{1},c:{2},n:{3}", item.A, item.B, item.C, n);
}
Action<int, int, Action<int>> myfor = (start, end, callback) => {
for (int i = start; i < end; i++) {
callback(i);
}
};
myfor(1, n, (a) => myfor(1, n, (b) => myfor(1, n, (c) => {
if (a * a + b * b == c * c)
Console.WriteLine("a:{0},b:{1},c:{2},n:{3}", a, b, c, n);
})));
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
for (int c = 1; c < n; c++) {
if (a * a + b * b == c * c)
Console.WriteLine("a:{0},b:{1},c:{2},n:{3}", a, b, c, n);
}
}
}
var r = from a in Enumerable.Range(1, n)
from b in Enumerable.Range(a, n)
from c in Enumerable.Range(b, n)
where a + b + c < n &&
a * a + b * b == c * c
select new { A = a, B = b, C = c };
foreach (var item in r) {
Console.WriteLine("a:{0},b:{1},c:{2},n:{3}", item.A, item.B, item.C, n);
}
给你一个数N,让你求出 这样的组合A,B,C,A,B,C要满足如下条件:A*A + B*B = C*C,A+B+C<=N
最后一种方法的结果和前两种的结果不一样,不知道为啥。