HDOJ 1002 大数相加C#编程求助

cjl3185328 2020-09-10 11:31:18
初学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();
}
}
}
}
...全文
1006 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjl3185328 2020-09-11
  • 打赏
  • 举报
回复
引用 1 楼 ziqi0716的回复:
既然初学,那就赶紧换语言去学java,Python,Go等等吧,我们49年入了国军,你就别来趟浑水了
是初学,因为工作要用,我也想用c++呀
ziqi0716 2020-09-11
  • 打赏
  • 举报
回复
既然初学,那就赶紧换语言去学java,Python,Go等等吧,我们49年入了国军,你就别来趟浑水了

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧