求极限求无限数相加算法

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace AnyNumberPlus2
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 string num1 = "0" + "145435435";
12 string num2 = "0" + "99";
13 string resault = num1.Length <num2.Length ? num1=num1.PadLeft(num2.Length,'0') : num2=num2.PadLeft(num1.Length,'0');
14 int flag = 0;
15 resault = "";
16 for (int i = num1.Length - 1; i >= 0; i--)
17 {
18 string res = ((int.Parse(num1[i].ToString()) + int.Parse(num2[i].ToString())) % 10 + flag).ToString();
19 resault = res + resault;
20 flag = (int.Parse(num1[i].ToString()) + int.Parse(num2[i].ToString())) >= 10 ? 1 : 0;
21 }
22 resault = resault.StartsWith("0") == true ? resault.Substring(1, resault.Length - 1) : resault;
23 Console.WriteLine(resault);
24 Console.ReadLine();
25 }
26 }
27 }
posted @ 2011-07-12 19:23  晴天娃娃、  阅读(521)  评论(0编辑  收藏  举报