# C#实现大数相加

``` 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
6
7 namespace ConsoleApplication1
8 {
9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入第一个加数");
15             Console.WriteLine("请输入第二个加数");
17
18             string result = TwoBigNumAdd(oneNum, twoNum);
19             Console.WriteLine(result);
20         }
21
22         static string TwoBigNumAdd(string a, string b)
23         {
24             int k = 0;
25             List<int> array = new List<int>();
26             List<int> one = new List<int>();
27             List<int> two = new List<int>();
28
29             //将两个数处理成相同长度的字符串，短的小的数字前面补0
30             for (int i = 0; i < (a.Length > b.Length ? a.Length : b.Length); i++)
31             {
32                 if (i >= a.Length)
33                     one.Insert(i - a.Length, 0);
34                 else
36                 if (i >= b.Length)
37                     two.Insert(i - b.Length, 0);
38                 else
40             }
41
42             //array集合用于存储相加的和，所以长度最大也只会比最大的数长度长1，刚开始全部存0
43             for (int i = 0; i <= (a.Length > b.Length ? a.Length : b.Length); i++)
44             {
46             }
47
48             //从低位往高位每位开始相加，如果相加 >=10 则进1取余
49             for (int i = (a.Length > b.Length ? a.Length : b.Length) - 1; i >= 0; i--)
50             {
51                 array[i + 1] += (one[i] + two[i]) % 10;
52                 k = (one[i] + two[i]) / 10;
53
54                 array[i] += k;
55             }
56
57             //如果首位为0，则移除
58             if (array[0] == 0)
59             {
60                 array.RemoveAt(0);
61             }
62
63             //将集合转换成字符串返回
64             StringBuilder result = new StringBuilder();
65             for (int i = 0; i < array.Count; i++)
66             {
67                 result.Append(array[i]);
68             }
69             return result.ToString();
70         }
71     }
72 }```

## 模板，大数相加

char a[Max],b[Max],c[Max],sum[Max]; void jia(char str1[],char str2[]) { int i,j,k,z; k=0;z=0; for(i=strlen(str1)-1,j=strlen(str2)-1;i>=0||j>=0;i--,j--) //核心,加法以及进位 { if(i>=0) z+=str1[i]-'0'; if(j>=0) z+=str2[j]-'0'; c[k++]=z%10+'0'; z=z/10; }

## 两个大数相加 ----Javascrit 实现

(function(){ var addLarge = function(n1,n2){ var over = 0; var ret = ""; var len = Math.min(n1.length,n2.length); var sln1 = n1.substr(n1.length - len,n1.length ); var sln2 = n2.substr(n2.length - len,n2.length ); for(var i = len;i > 0; i--)

## hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex