永发信息网

杭电HDU 1002问题 java写的一直wrong answer 一下午加一晚上 崩溃了快 救命啊

答案:2  悬赏:60  手机版
解决时间 2021-02-13 17:26
  • 提问者网友:难遇难求
  • 2021-02-13 06:48
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"); } } }
最佳答案
  • 五星知识达人网友:深街酒徒
  • 2021-02-13 08:07
输入格式不完全按照题目说的来
所以一行一行的读会出错
用一个一个token的读就可以无视输入格式了
全部回答
  • 1楼网友:夜风逐马
  • 2021-02-13 08:52
你好! 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 ) 如有疑问,请追问。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯