一个关于从1到100的加法算法
今天在csdn上看到这么一个问题:
从1到100,100个数字相加,和为100的算法,可以1位,2位,3位等,不限位数相加,数字不能重复,可以有多少种算法,并把代码贴出;
可以这样:
1 + 99;
2 + 98;
也可以这样:
1 + 2 + 97;
更可以这样:
1 + 2 + 3 + 5 + 7 + 82;
随手用C#写了一下实现代码,部分结果如下:
...
1+2+3+4+5+6+7+8+9+10+14+15+16
1+2+3+4+5+6+7+8+9+11+12+13+19
1+2+3+4+5+6+7+8+9+11+12+14+18
1+2+3+4+5+6+7+8+9+11+12+15+17
1+2+3+4+5+6+7+8+9+11+13+14+17
1+2+3+4+5+6+7+8+9+11+13+15+16
1+2+3+4+5+6+7+8+9+12+13+14+16
1+2+3+4+5+6+7+8+10+11+12+13+18
1+2+3+4+5+6+7+8+10+11+12+14+17
1+2+3+4+5+6+7+8+10+11+12+15+16
1+2+3+4+5+6+7+8+10+11+13+14+16
1+2+3+4+5+6+7+8+10+12+13+14+15
1+2+3+4+5+6+7+9+10+11+12+13+17
1+2+3+4+5+6+7+9+10+11+12+14+16
1+2+3+4+5+6+7+9+10+11+13+14+15
1+2+3+4+5+6+8+9+10+11+12+13+16
1+2+3+4+5+6+8+9+10+11+12+14+15
1+2+3+4+5+7+8+9+10+11+12+13+15
1+2+3+4+6+7+8+9+10+11+12+13+14
Start Time: 2006-9-5 10:53:24
End Time: 2006-9-5 10:58:39
Total Time: 00:05:14.3906250
Total Count: 444792
如果不输出式子,结果如下:
Start Time: 2006-9-5 11:01:13
End Time: 2006-9-5 11:01:14
Total Time: 00:00:01.8750000
Total Count: 444792
完整代码如下:
从1到100,100个数字相加,和为100的算法,可以1位,2位,3位等,不限位数相加,数字不能重复,可以有多少种算法,并把代码贴出;
可以这样:
1 + 99;
2 + 98;
也可以这样:
1 + 2 + 97;
更可以这样:
1 + 2 + 3 + 5 + 7 + 82;
随手用C#写了一下实现代码,部分结果如下:
...
1+2+3+4+5+6+7+8+9+10+14+15+16
1+2+3+4+5+6+7+8+9+11+12+13+19
1+2+3+4+5+6+7+8+9+11+12+14+18
1+2+3+4+5+6+7+8+9+11+12+15+17
1+2+3+4+5+6+7+8+9+11+13+14+17
1+2+3+4+5+6+7+8+9+11+13+15+16
1+2+3+4+5+6+7+8+9+12+13+14+16
1+2+3+4+5+6+7+8+10+11+12+13+18
1+2+3+4+5+6+7+8+10+11+12+14+17
1+2+3+4+5+6+7+8+10+11+12+15+16
1+2+3+4+5+6+7+8+10+11+13+14+16
1+2+3+4+5+6+7+8+10+12+13+14+15
1+2+3+4+5+6+7+9+10+11+12+13+17
1+2+3+4+5+6+7+9+10+11+12+14+16
1+2+3+4+5+6+7+9+10+11+13+14+15
1+2+3+4+5+6+8+9+10+11+12+13+16
1+2+3+4+5+6+8+9+10+11+12+14+15
1+2+3+4+5+7+8+9+10+11+12+13+15
1+2+3+4+6+7+8+9+10+11+12+13+14
Start Time: 2006-9-5 10:53:24
End Time: 2006-9-5 10:58:39
Total Time: 00:05:14.3906250
Total Count: 444792
如果不输出式子,结果如下:
Start Time: 2006-9-5 11:01:13
End Time: 2006-9-5 11:01:14
Total Time: 00:00:01.8750000
Total Count: 444792
完整代码如下:
///jailusd@hotmail.com
///2006-09-05
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace OneToHundred
{
class Program
{
static ArrayList alFormula = new ArrayList();
static string strTempFormula;
static void Main(string[] args)
{
DateTime dtStart = DateTime.Now;
AddFormula("",1,100);
for (int i = 0; i < alFormula.Count; i++)
{
string strTemp = alFormula[i].ToString();
string[] str = strTemp.Split("+".ToCharArray());
strTemp = strTemp.Substring(0, strTemp.Length - str[str.Length - 1].ToString().Length);
AddFormula(strTemp, int.Parse(str[str.Length - 2]) + 1, int.Parse(str[str.Length - 1]));
}
DateTime dtEnd = DateTime.Now;
TimeSpan ts = DateTime.Now.Subtract(dtStart);
double count= ts.TotalMilliseconds;
Console.WriteLine("Start Time:\t" + dtStart);
Console.WriteLine("End Time:\t" + dtEnd);
Console.WriteLine("Total Time:\t" + ts);
Console.WriteLine("Total Count:\t" + alFormula.Count);
Console.ReadLine();
}
static void AddFormula(string Formual,int Start, int End)
{
int intTemp = (int)Math.Round((double)(End / 2));
if (End % 2 != 0)
{
intTemp += 1;
}
for (int i = Start; i < intTemp; i++)
{
strTempFormula = Formual + i.ToString() + "+" + (End - i).ToString();
//Console.WriteLine(strTempFormula); //显示每个满足条件的式子
alFormula.Add(strTempFormula);
}
}
}
}
///2006-09-05
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace OneToHundred
{
class Program
{
static ArrayList alFormula = new ArrayList();
static string strTempFormula;
static void Main(string[] args)
{
DateTime dtStart = DateTime.Now;
AddFormula("",1,100);
for (int i = 0; i < alFormula.Count; i++)
{
string strTemp = alFormula[i].ToString();
string[] str = strTemp.Split("+".ToCharArray());
strTemp = strTemp.Substring(0, strTemp.Length - str[str.Length - 1].ToString().Length);
AddFormula(strTemp, int.Parse(str[str.Length - 2]) + 1, int.Parse(str[str.Length - 1]));
}
DateTime dtEnd = DateTime.Now;
TimeSpan ts = DateTime.Now.Subtract(dtStart);
double count= ts.TotalMilliseconds;
Console.WriteLine("Start Time:\t" + dtStart);
Console.WriteLine("End Time:\t" + dtEnd);
Console.WriteLine("Total Time:\t" + ts);
Console.WriteLine("Total Count:\t" + alFormula.Count);
Console.ReadLine();
}
static void AddFormula(string Formual,int Start, int End)
{
int intTemp = (int)Math.Round((double)(End / 2));
if (End % 2 != 0)
{
intTemp += 1;
}
for (int i = Start; i < intTemp; i++)
{
strTempFormula = Formual + i.ToString() + "+" + (End - i).ToString();
//Console.WriteLine(strTempFormula); //显示每个满足条件的式子
alFormula.Add(strTempFormula);
}
}
}
}