HDU1002Java两种解 AC
Problem Description:题目来源
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
.
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
Author
Ignatius.L
题目大概意思就是输入两个超大整数(大到若用基本数据类型则会溢出)对这两个数求和。
思路:这个问题难点就是这两个数可以非常大,32位的int型无法存储。
方法1:与c/c++不同Java中提供了BigInteger类来处理超大整数,BigDecimal类处理超大浮点数,所以第一种方法就是使用BigInteger类,非常简单,但是要注意题目的输出格式,最后一个Case输出结果后不要有空行,还有特别注意输出内容的大小写。
下附Java AC 代码:
要特比注意输出个数
import java.util.Scanner;//引入Scanner
import java.math.BigInteger;//引入BigInteger
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
BigInteger number1,number2;
for(int i = 1;i <= t; i++){
System.out.println("Case "+i+":");
number1 = sc.nextBigInteger();
number2 = sc.nextBigInteger();
System.out.println(number1+" + "+number2+" = "+number1.add(number2));
if(i < t) System.out.println();
}
}
}
方法2:有些题目可能会有要求说不能使用BigInteger类,这是解决思路也并不太难,主要看个人对java字符串各种操作的掌握情况。
因为基本数据类型位数不足以存储超大整数,所以选择以字符串形式存储大整数,然后诸位相加,再输出结果,具体过程再代码注释中解释。
附Java AC 代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 1;i <= t;i++){
System.out.println("Case "+i+":");
String number1 = sc.next();
String number2 = sc.next();
String result =addBigNumber(number1,number2);
System.out.println(number1+" + "+number2+" = "+result);
if( i < t) System.out.println();
}
}
public static String addBigNumber(String num1,String num2){
StringBuffer add = new StringBuffer();
/*逆序存储输入的字符串
* 目的:两字符串长度不相同时将短字符串用0补齐
* 因为数字是高为补0,字符串方便在字符串后补字符,所以先逆序*/
String n1 = new StringBuffer(num1).reverse().toString();
String n2 = new StringBuffer(num2).reverse().toString();
/*获取两字符串数字最大长度*/
int maxLen = n1.length() > n2.length() ? n1.length():n2.length();
/*如果n1比n2短,则n1高位用"0"补齐*/
if(n1.length() < n2.length()){
for(int i = n1.length(); i < n2.length(); i++){
n1 += "0";
}
}
/*如果n2比n1短,则n2高位用"0"补齐*/
if(n1.length() > n2.length()){
for(int i = n2.length(); i < n1.length(); i++){
n2 += "0";
}
}
int flag = 0;//进位标志
boolean overFlow = false;//是否溢出,即加法结束后数字长度有没有变长
for(int i = 0; i < maxLen; i++){
/*将字符串按字符转换为数字再进行加法运算*/
int sum = Integer.parseInt(n1.charAt(i)+"")+Integer.parseInt(n2.charAt(i)+"")+flag;
/*若相加大于等于10,则进位,结果字符串中加入sum-10*/
if(sum >= 10){
/*如果i是最高为则溢出*/
if(i == maxLen){
overFlow = true;
}
flag = 1;
add.append(sum-10);
}else{
/*若相加小于10,则不进位,结果字符串中加入sum*/
flag = 0;
add.append(sum);
}
}
//有溢出则最高为再加1
if (overFlow){
add.append(flag);
}
//最后返回逆序字符串,结果字符串就正序了
return add.reverse().toString();
}
}
欢迎留言。