问题描述
求两个不超过 200 位的非负整数的和。
输入
有两行,每行是一个不超过 200 位的非负整数,没有多余的前导 0。
输出
一行,即相加后的结果。结果不能有多余的前导 0,即如果结果是 342,那么就不能输出为 0342。
输入样列
22222222222222222222
33333333333333333333
输出样例
55555555555555555555
算法实现
using System;
namespace Questions{
class Program{
public static void Main(string[] args){
string m = Console.ReadLine();
string n = Console.ReadLine();
int l = m.Length > n.Length ? m.Length+1 : n.Length+1;
int[] k=new int[400];
if (m.Length > n.Length) {
string temp = m;
m = n;
n = temp ;
}
for (int i = 0; i < m.Length ; i++) {
int temp = n[n.Length - i - 1] + m[m.Length - i - 1] - '0' - '0'+ k[i];
if (temp >=10) {
k[i + 1]++;
k[i] = temp - 10;
}else
k[i] = temp;
}
for (int i = m.Length; i < n.Length; i++)
{
int temp = k[i] + n[n.Length - i - 1] - '0';
if (temp >= 10)
{
k[i + 1]++;
k[i] = temp - 10;
}
else
k[i] = temp;
}
while (k[l - 1] == 0)
l--;
for (int i = l-1; i >= 0; i--)
Console.Write(k[i]);
Console.WriteLine();
Console.ReadKey();
}
}
}