初學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();
}
}
}
}
uj5u.com熱心網友回復:
既然初學,那就趕緊換語言去學java,Python,Go等等吧,我們49年入了國軍,你就別來趟渾水了uj5u.com熱心網友回復:
是初學,因為作業要用,我也想用c++呀
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/8438.html
標籤:C#
