中易网

C# 如何获得多个数组合的所有情况

答案:1  悬赏:0  
解决时间 2021-01-16 21:46
  • 提问者网友:精神病院里
  • 2021-01-15 20:56
C# 如何获得多个数组合的所有情况
最佳答案
  • 二级知识专家网友:持酒劝斜阳
  • 2021-01-15 21:23
static int[] number;
static int[] result;
///
/// 应用程序的主入口点。
///

[STAThread]
static void Main(string[] args)
{
number = new int[] {2,3,4,6};
result = new int[number.Length];
Backtrace(0);
System.Console.Read();
}
static void Backtrace(int i)
{
if (i >= number.Length)
{
//没有数字可选
return;
}
result[i] = 1; //选中当前数字
PrintResult(); //输出结果
Backtrace(i+1); //选择下一数字
result[i] = 0; //剔除当前数字
Backtrace(i+1); //选择下一数字
}
static void PrintResult()
{
int count = 0;
int total = 0;
string msg = "";
for (int i = 0; i < result.Length; i++)
{
count += result[i];
if (result[i] == 1)
{
if (count > 1)
{
msg += "+";
}
total += number[i];
msg += number[i];
}
}
if (count > 1)
{
System.Console.WriteLine(msg + "=" + total);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息