杭电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"); } } }
展开
2个回答
展开全部
while (line[j] != ' '&&j < line.length)改为
while (j < line.length&&line[j] != ' ')
while (ans[length - 1] == 0 &&length > 1 )改为
while (length > 1&&ans[length - 1] == 0 )
while (j < line.length&&line[j] != ' ')
while (ans[length - 1] == 0 &&length > 1 )改为
while (length > 1&&ans[length - 1] == 0 )
追问
还是WA 你这个改了和没改的没区别吧
追答
你可以打断点调试一下,看那一步和预想有出入
在上面基础上把:while(j < line.length) 改为
while (j < line.length&&line[j] != ' ')
你是不是想实现:
12 34
12+34=46
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询