HDOJ 1002 大数相加C#编程求助
初学C#,本着练手和熟悉代码的想法去刷刷题,结果以前认为很简单的题一直WA,求助同学后知道是2450584 257这组数据卡住了,但是都不知道该怎么解决,故此求助,代码附上
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HDOJ_A_Plus_B_CSharp2
{
class HDOJ1002
{
public static void Main(string[] args)
{
int n;
string line;
while ((line = Console.ReadLine()) != null)
{
n = int.Parse(line);
for (int t = 0;t < n;t ++ )
{
int[] sum = new int[100000];
for (int i1 = 0; i1 < 100000; i1++)
{
sum[i1] = 0;
}
string line1;
string[] str;
string a=null, b=null;
int lena, lenb;
int cnt, k, k1;
line1 = Console.ReadLine();
str = line1.Split(' ');
a = str[0];
b = str[1];
lena = a.Length;
lenb = b.Length;
cnt = 0;
k = lena >= lenb ? lena : lenb;
k1 = k;
int i, j;
// sum[k + 1] = 0;
for (i = lena - 1, j = lenb - 1; i >= 0 && j >= 0; i--, j--)
{
sum[k] = (cnt + a[i] + b[i] - '0' - '0') % 10;
cnt = (a[i] + b[j] - '0' - '0' + cnt) / 10;
k--;
}
if (lena > lenb)
{
for (; i >= 0; i--)
{
sum[k] = (a[i] - '0' + cnt) % 10;
cnt = (a[i] - '0' + cnt) / 10;
k--;
}
}
else
{
for (; j >= 0; j--)
{
sum[k] = (b[j] - '0' + cnt) % 10;
cnt = (b[j] - '0' + cnt) / 10;
k--;
}
}
Console.WriteLine("Case {0}:", t+1);
if (cnt == 0)
{
Console.Write("{0} + {1} = ", a, b);
for (int i1 = 1; i1 <= k1; i1++)
Console.Write(sum[i1]);
Console.WriteLine("");
Console.WriteLine("");
}
else
{
sum[0] = cnt;
Console.Write("{0} + {1} = ", a, b);
for (int i1 = 0; i1 <= k1; i1++)
Console.Write(sum[i1]);
Console.WriteLine("");
Console.WriteLine("");
}
}
//Console.ReadKey();
}
}
}
}