a + b + c + d + e = N求解代码
题目:a+b+c+d+e+f=n n这个值是自个输入的..求a,b,c,d,e,f的值.
解答程序(第一份程序,未做任何优化):
几组输出数据:
10
1 + 1 + 1 + 1 + 6 = 10
1 + 1 + 1 + 2 + 5 = 10
1 + 1 + 1 + 3 + 4 = 10
1 + 1 + 2 + 2 + 4 = 10
1 + 1 + 2 + 3 + 3 = 10
1 + 2 + 2 + 2 + 3 = 10
2 + 2 + 2 + 2 + 2 = 10
Spent time: 0s 0ms
Result count: 7
------------------------------------------------------------------
11
1 + 1 + 1 + 1 + 7 = 11
1 + 1 + 1 + 2 + 6 = 11
1 + 1 + 1 + 3 + 5 = 11
1 + 1 + 1 + 4 + 4 = 11
1 + 1 + 2 + 2 + 5 = 11
1 + 1 + 2 + 3 + 4 = 11
1 + 1 + 3 + 3 + 3 = 11
1 + 2 + 2 + 2 + 4 = 11
1 + 2 + 2 + 3 + 3 = 11
2 + 2 + 2 + 2 + 3 = 11
Spent time: 0s 0ms
Result count: 10
-----------------------------------------------
120
Spent time: 0s 488ms
Result count: 77686
解答程序(第一份程序,未做任何优化):
using System;
using System.Collections.Generic;
using System.Text;
namespace FiveNumber
{
class Program
{
static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
StringBuilder sb = new StringBuilder();
int i = 0;
int temp = N > 100 ? 100 : N;
int tempA, tempB, tempC, tempD, tempE;
tempA = N / 5;
for (int a = 1; a < temp; a++)
{
if (a > tempA)
{
break;
}
tempB = ((N - (a)) / 2);
for (int b = a; b < temp; b++)
{
if ((b - a) > tempB)
{
break;
}
tempC = ((N - (a + b)) / 2);
for (int c = b; c < temp; c++)
{
if ((c - b) > tempC)
{
break;
}
tempD = ((N - (a + b + c)) / 2);
for (int d = c; d < temp; d++)
{
if ((d - c) > tempD)
{
break;
}
tempE = ((N - (a + b + c + d)) / 2);
for (int e = d; e < temp; e++)
{
if (a + b + c + d + e == N)
{
i++;
sb.Append(string.Format("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, N));
sb.Append(Environment.NewLine);
if ((e - d) > tempE)
{
break;
}
}
else if (a + b + c + d + e > N)
{
break;
}
}
}
}
}
}
sw.Stop();
//显示所有结果
//Console.WriteLine(sb.ToString());
//总耗时
Console.WriteLine(string.Format("Spent time: {0}s {1}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds));
//总记录数
Console.WriteLine(string.Format("Result count: {0}", i.ToString()));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace FiveNumber
{
class Program
{
static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
StringBuilder sb = new StringBuilder();
int i = 0;
int temp = N > 100 ? 100 : N;
int tempA, tempB, tempC, tempD, tempE;
tempA = N / 5;
for (int a = 1; a < temp; a++)
{
if (a > tempA)
{
break;
}
tempB = ((N - (a)) / 2);
for (int b = a; b < temp; b++)
{
if ((b - a) > tempB)
{
break;
}
tempC = ((N - (a + b)) / 2);
for (int c = b; c < temp; c++)
{
if ((c - b) > tempC)
{
break;
}
tempD = ((N - (a + b + c)) / 2);
for (int d = c; d < temp; d++)
{
if ((d - c) > tempD)
{
break;
}
tempE = ((N - (a + b + c + d)) / 2);
for (int e = d; e < temp; e++)
{
if (a + b + c + d + e == N)
{
i++;
sb.Append(string.Format("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, N));
sb.Append(Environment.NewLine);
if ((e - d) > tempE)
{
break;
}
}
else if (a + b + c + d + e > N)
{
break;
}
}
}
}
}
}
sw.Stop();
//显示所有结果
//Console.WriteLine(sb.ToString());
//总耗时
Console.WriteLine(string.Format("Spent time: {0}s {1}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds));
//总记录数
Console.WriteLine(string.Format("Result count: {0}", i.ToString()));
Console.ReadLine();
}
}
}
几组输出数据:
10
1 + 1 + 1 + 1 + 6 = 10
1 + 1 + 1 + 2 + 5 = 10
1 + 1 + 1 + 3 + 4 = 10
1 + 1 + 2 + 2 + 4 = 10
1 + 1 + 2 + 3 + 3 = 10
1 + 2 + 2 + 2 + 3 = 10
2 + 2 + 2 + 2 + 2 = 10
Spent time: 0s 0ms
Result count: 7
------------------------------------------------------------------
11
1 + 1 + 1 + 1 + 7 = 11
1 + 1 + 1 + 2 + 6 = 11
1 + 1 + 1 + 3 + 5 = 11
1 + 1 + 1 + 4 + 4 = 11
1 + 1 + 2 + 2 + 5 = 11
1 + 1 + 2 + 3 + 4 = 11
1 + 1 + 3 + 3 + 3 = 11
1 + 2 + 2 + 2 + 4 = 11
1 + 2 + 2 + 3 + 3 = 11
2 + 2 + 2 + 2 + 3 = 11
Spent time: 0s 0ms
Result count: 10
-----------------------------------------------
120
Spent time: 0s 488ms
Result count: 77686
浙公网安备 33010602011771号