比如,写个简单的C#例子,要求如下:
输入2个数字 第一个为范围,第2个为总数
要求 在1-范围内 找到所有相加等于总数的集合(集合必须为5)并且这5个数字在本组内不能重复
比如 范围=d 总数=c
5个数分别是a1,a2,a3,a4,a5
必须满足 a1<d a2<d a3<d a4<d a5<d 并且 a1!=a2!=a3!=a4!=a5 并且 a1+a2+a3+a4+a5=c看谁的例子最简洁.希望大家集思广益多多发言.
输入2个数字 第一个为范围,第2个为总数
要求 在1-范围内 找到所有相加等于总数的集合(集合必须为5)并且这5个数字在本组内不能重复
比如 范围=d 总数=c
5个数分别是a1,a2,a3,a4,a5
必须满足 a1<d a2<d a3<d a4<d a5<d 并且 a1!=a2!=a3!=a4!=a5 并且 a1+a2+a3+a4+a5=c看谁的例子最简洁.希望大家集思广益多多发言.
解决方案 »
- web控件网页的go method标记问题
- 输入23.45,如何得到23,做到只要整数部分
- 星星问问题,简单高分:如何使用命令方式调用 script wizard
- c#的winform下有没有像delphi的hotimage那样的控件,可以把图片分为多个热区
- 急!!!!!!!!在线等!!!!!!!!!得到一个文件流(stream)后,如何把他存成相应的文件.
- 请教:如何做一个类似监视窗口的功能监控程序中各个变量的值???
- 一个英语问题,篮球中的“盖帽”因改怎样翻译?
- ASP.net 数据库连接 SQL
- 为什么Class1没有构造函数
- 关于D3的问题
- 如何给Web下的TextBox加提示层
- C# 操作 VF 数据库问题
d>5&&c>15&&5d-10>c
算法等下再说
{
for (int b = a + 1; b <= max; b++)
{
for (int c = b + 1; c <= max; c++)
{
for (int d = c + 1; d <= max; d++)
{
for (int e = d + 1; e <= max; e++)
{
if (a + b + c + d + e == total)
{
Console.WriteLine("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, total);
}
}
}
}
}
}
{
for (int b = a + 1; b < max; b++)
{
for (int c = b + 1; c < max; c++)
{
for (int d = c + 1; d < max; d++)
{
for (int e = d + 1; e < max; e++)
{
if (a + b + c + d + e == total)
{
Console.WriteLine("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, total);
}
}
}
}
}
}
{
for (int b = a + 1; b < max; b++)
{
for (int c = b + 1; c < max; c++)
{
for (int d = c + 1; d < max; d++)
{
for (int e = d + 1; e < max; e++)
{
if (a + b + c + d + e == total)
{
Console.WriteLine("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, total);
break;//还是加一个好
}
}
}
}
}
}
for (int i = 0; i < count; i++)
{
result[i] = i + 1;
}while (result[0] < max - count)
{
for (int i = count - 1; i >= 0; i--)
{
if (result[i] < max - count + i)
{
result[i]++;
for (int j = i + 1; j < count; j++)
{
result[j] = result[j - 1] + 1;
}
break;
}
} int sum = 0;
foreach (int i in result) sum += i;
if (sum == total)
{
foreach (int i in result) Console.Write("{0} ", i);
Console.Write("= {0}\r\n", total);
}
}