杭电HDU 1002问题 java为什么一直wrong answer?! 输出结果正确 格式正确 前导零和进位也都考虑了啊 疯了
importjava.io.*;importjava.util.*;publicclassHDU1002{publicstaticvoidmain(String[]arg...
import java.io.*; import java.util.*; public class HDU1002 { public static void main(String[] args) throws IOException { Scanner sca = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = sca.nextInt(); for(int i = 0; i < n; i++) { int[] num1 = new int[1024]; int[] num2 = new int[1024]; int[] ans = new int[1024]; int borrow = 0; String str = br.readLine(); char[] line = str.toCharArray(); int j = 0, k = 0; int length,length1,length2; while(line[j] != ' ' && j < line.length) ++j; for(int l = j - 1; l >= 0; l--) { num1[k] = (int)line[l] - 48; ++k; } length1 = k; k = 0; ++j; int s = j; while(j < line.length) ++j; for(int l = j - 1; l >= s; l--) { num2[k] = (int)line [l] - 48; ++k; } length2 = k; j = 0; while(j < length1 && j < length2) { ans[j] = num1[j] + num2[j] + borrow; borrow = ans[j] / 10; ans[j] %= 10; ++j; } if(j >= length1) { while(j < length2) { ans[j] = num2[j] + borrow; borrow = ans[j] / 10; ans[j] %= 10; ++j; } } if(j >= length2) { while(j < length1) { ans[j] = num1[j] + borrow; borrow = ans[j] / 10; ans[j] %= 10; ++j; } } if(borrow == 1) { ans[j] = 1; ++j; } length = j; while(ans[length - 1] == 0 && length > 1) --length; System.out.println("Case " + (i + 1) + ":"); for(j = length1 - 1; j >= 0; j--) System.out.print(num1[j]); System.out.print(" + "); for(j = length2 - 1; j >= 0; j--) System.out.print(num2[j]); System.out.print(" = "); for(j = length - 1; j >= 0; j--) System.out.print(ans[j]); System.out.println(); if(i != n - 1) System.out.printf("\r\n"); } } }
展开
1个回答
展开全部
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
Scanner sca = new Scanner(System.in);
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = sca.nextInt();
for(int i = 0; i < n; i++)
{
int[] num1 = new int[1024];
int[] num2 = new int[1024];
int[] ans = new int[1024];
int borrow = 0;
String str = sca.next();
String str2 = sca.next();
str = str + " " + str2;
char[] line = str.toCharArray();
int j = 0, k = 0;
int length,length1,length2;
while(line[j] != ' ' && j < line.length)
++j;
for(int l = j - 1; l >= 0; l--)
{
num1[k] = (int)line[l] - 48;
++k;
}
length1 = k;
k = 0;
++j;
int s = j;
j = line.length;
for(int l = j - 1; l >= s; l--)
{
num2[k] = (int)line [l] - 48;
++k;
}
length2 = k;
j = 0;
while(j < length1 && j < length2)
{
ans[j] = num1[j] + num2[j] + borrow;
borrow = ans[j] / 10;
ans[j] %= 10;
++j;
}
if(j >= length1)
{
while(j < length2)
{
ans[j] = num2[j] + borrow;
borrow = ans[j] / 10;
ans[j] %= 10;
++j;
}
}
if(j >= length2)
{
while(j < length1)
{
ans[j] = num1[j] + borrow;
borrow = ans[j] / 10;
ans[j] %= 10;
++j;
}
}
if(borrow == 1)
{
ans[j] = 1;
++j;
}
length = j;
while(ans[length - 1] == 0 && length > 1)
--length;
System.out.println("Case " + (i + 1) + ":");
for(j = length1 - 1; j >= 0; j--)
System.out.print(num1[j]);
System.out.print(" + ");
for(j = length2 - 1; j >= 0; j--)
System.out.print(num2[j]);
System.out.print(" = ");
for(j = length - 1; j >= 0; j--)
System.out.print(ans[j]);
System.out.println();
if(i != n - 1)
System.out.println();
}
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
Scanner sca = new Scanner(System.in);
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = sca.nextInt();
for(int i = 0; i < n; i++)
{
int[] num1 = new int[1024];
int[] num2 = new int[1024];
int[] ans = new int[1024];
int borrow = 0;
String str = sca.next();
String str2 = sca.next();
str = str + " " + str2;
char[] line = str.toCharArray();
int j = 0, k = 0;
int length,length1,length2;
while(line[j] != ' ' && j < line.length)
++j;
for(int l = j - 1; l >= 0; l--)
{
num1[k] = (int)line[l] - 48;
++k;
}
length1 = k;
k = 0;
++j;
int s = j;
j = line.length;
for(int l = j - 1; l >= s; l--)
{
num2[k] = (int)line [l] - 48;
++k;
}
length2 = k;
j = 0;
while(j < length1 && j < length2)
{
ans[j] = num1[j] + num2[j] + borrow;
borrow = ans[j] / 10;
ans[j] %= 10;
++j;
}
if(j >= length1)
{
while(j < length2)
{
ans[j] = num2[j] + borrow;
borrow = ans[j] / 10;
ans[j] %= 10;
++j;
}
}
if(j >= length2)
{
while(j < length1)
{
ans[j] = num1[j] + borrow;
borrow = ans[j] / 10;
ans[j] %= 10;
++j;
}
}
if(borrow == 1)
{
ans[j] = 1;
++j;
}
length = j;
while(ans[length - 1] == 0 && length > 1)
--length;
System.out.println("Case " + (i + 1) + ":");
for(j = length1 - 1; j >= 0; j--)
System.out.print(num1[j]);
System.out.print(" + ");
for(j = length2 - 1; j >= 0; j--)
System.out.print(num2[j]);
System.out.print(" = ");
for(j = length - 1; j >= 0; j--)
System.out.print(ans[j]);
System.out.println();
if(i != n - 1)
System.out.println();
}
}
}
追问
你去我提问的另一个问题吧 这个是提问的时候多发了一次 所以没分 你把那个答一下 把分给你 http://zhidao.baidu.com/question/430152077.html?quesup2&oldq=1
最好能给我解答下为什么不能用BufferedReader哈~
追答
已回答
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询