一个关于从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

完整代码如下:
ExpandedBlockStart.gif ContractedBlock.gif /**/ ///jailusd@hotmail.com
ExpandedBlockEnd.gif
///2006-09-05

None.gif
None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  OneToHundred
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static ArrayList alFormula = new ArrayList();
InBlock.gif        
static string strTempFormula;
InBlock.gif
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DateTime dtStart 
= DateTime.Now;
InBlock.gif
InBlock.gif            AddFormula(
"",1,100);
InBlock.gif
InBlock.gif            
for (int i = 0; i < alFormula.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strTemp = alFormula[i].ToString();
InBlock.gif                
string[] str = strTemp.Split("+".ToCharArray());
InBlock.gif
InBlock.gif                strTemp 
= strTemp.Substring(0, strTemp.Length - str[str.Length - 1].ToString().Length);
InBlock.gif
InBlock.gif                AddFormula(strTemp, 
int.Parse(str[str.Length - 2]) + 1int.Parse(str[str.Length - 1]));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            DateTime dtEnd 
= DateTime.Now;
InBlock.gif
InBlock.gif            TimeSpan ts 
= DateTime.Now.Subtract(dtStart);
InBlock.gif            
double count= ts.TotalMilliseconds;
InBlock.gif
InBlock.gif            Console.WriteLine(
"Start Time:\t" + dtStart);
InBlock.gif            Console.WriteLine(
"End Time:\t" + dtEnd);
InBlock.gif            Console.WriteLine(
"Total Time:\t" + ts);
InBlock.gif            Console.WriteLine(
"Total Count:\t" + alFormula.Count);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static void AddFormula(string Formual,int Start, int End)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int intTemp = (int)Math.Round((double)(End / 2));
InBlock.gif
InBlock.gif            
if (End % 2 != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intTemp 
+= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for (int i = Start; i < intTemp; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strTempFormula 
= Formual + i.ToString() + "+" + (End - i).ToString();
InBlock.gif                
//Console.WriteLine(strTempFormula);    //显示每个满足条件的式子
InBlock.gif
                alFormula.Add(strTempFormula);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值