I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
解答要求
时间限制:1000ms, 内存限制:100MB
输入
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.
输出
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.
样例
输入样例 1 复制
2
1 2
112233445566778899 998877665544332211
输出样例 1
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取测试用例数量
int T = scanner.nextInt();
scanner.nextLine(); // 读取换行符
// 处理每个测试用例
for (int testCase = 1; testCase <= T; testCase++) {
// 读取两个大整数 A 和 B
String inputA = scanner.next();
String inputB = scanner.next();
// 使用 BigInteger 处理大整数
BigInteger A = new BigInteger(inputA);
BigInteger B = new BigInteger(inputB);
// 计算和
BigInteger sum = A.add(B);
// 输出结果
System.out.println("Case " + testCase + ":");
System.out.println(inputA + " + " + inputB + " = " + sum);
// 输出测试用例之间的空白行,除了最后一个测试用例
if (testCase < T) {
System.out.println();
}
}
// 关闭 Scanner
scanner.close();
}
}
这个程序首先读取测试用例的数量,然后处理每个测试用例。在每个测试用例中,它使用 BigInteger
类型处理输入的大整数,计算它们的和,然后输出结果。程序使用循环和条件语句来处理多个测试用例,并在需要时输出空白行。
使用字符串模拟手动相加的过程,而不使用 BigInteger
。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取测试用例数量
int T = scanner.nextInt();
scanner.nextLine(); // 读取换行符
// 处理每个测试用例
for (int testCase = 1; testCase <= T; testCase++) {
// 读取两个大整数 A 和 B
String inputA = scanner.next();
String inputB = scanner.next();
// 计算和
String sum = addLargeNumbers(inputA, inputB);
// 输出结果
System.out.println("Case " + testCase + ":");
System.out.println(inputA + " + " + inputB + " = " + sum);
// 输出测试用例之间的空白行,除了最后一个测试用例
if (testCase < T) {
System.out.println();
}
}
// 关闭 Scanner
scanner.close();
}
// 大整数相加的函数
private static String addLargeNumbers(String num1, String num2) {
StringBuilder result = new StringBuilder();
int carry = 0;
// 补齐位数
while (num1.length() < num2.length()) {
num1 = "0" + num1;
}
while (num2.length() < num1.length()) {
num2 = "0" + num2;
}
// 从低位到高位逐位相加
for (int i = num1.length() - 1; i >= 0; i--) {
int digit1 = Character.getNumericValue(num1.charAt(i));
int digit2 = Character.getNumericValue(num2.charAt(i));
int tempSum = digit1 + digit2 + carry;
carry = tempSum / 10;
result.insert(0, tempSum % 10);
}
// 如果有进位,加上进位
if (carry > 0) {
result.insert(0, carry);
}
return result.toString();
}
}
这段代码定义了一个 addLargeNumbers
函数,用于实现大整数相加。在主程序中,它通过读取输入、调用相应的函数并输出结果,模拟了大整数的加法。
题目描述
I have a very simple problem for you again. Given two integers A and B, your job is to calculate the result of A - B.
解答要求
时间限制:1000ms, 内存限制:100MB
输入
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.
输出
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 = ?”, ? means the result of A - B.Note there are some spaces int the equation. Output a blank line between two test cases.
样例
输入样例 1 复制
3
9 8
12 8
123456789 987654321
输出样例 1
Case 1:
9 - 8 = 1
Case 2:
12 - 8 = 4
Case 3:
123456789 - 987654321 = -864197532
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取测试用例数量
int T = scanner.nextInt();
scanner.nextLine(); // 读取换行符
// 处理每个测试用例
for (int testCase = 1; testCase <= T; testCase++) {
// 读取两个大整数 A 和 B
String inputA = scanner.next();
String inputB = scanner.next();
// 使用 BigInteger 处理大整数
BigInteger A = new BigInteger(inputA);
BigInteger B = new BigInteger(inputB);
// 计算差值
BigInteger difference = A.subtract(B);
// 输出结果
System.out.println("Case " + testCase + ":");
System.out.println(inputA + " - " + inputB + " = " + difference);
// 输出测试用例之间的空白行,除了最后一个测试用例
if (testCase < T) {
System.out.println();
}
}
// 关闭 Scanner
scanner.close();
}
}
这段代码基本上与之前的代码相似,主要区别在于计算的操作是减法。它使用 BigInteger
类来处理大整数,然后输出结果。请注意,对于每个测试用例,输出中有一些空格,以符合题目要求。
不使用 BigInteger
来处理大整数,采用其他方法来实现大整数的加减法,可以使用字符串表示大整数,然后模拟手工计算的方式进行操作。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取测试用例数量
int T = scanner.nextInt();
scanner.nextLine(); // 读取换行符
// 处理每个测试用例
for (int testCase = 1; testCase <= T; testCase++) {
// 读取两个大整数 A 和 B
String inputA = scanner.next();
String inputB = scanner.next();
// 计算差值
String difference = subtractLargeNumbers(inputA, inputB);
// 输出结果
System.out.println("Case " + testCase + ":");
System.out.println(inputA + " - " + inputB + " = " + difference);
// 输出测试用例之间的空白行,除了最后一个测试用例
if (testCase < T) {
System.out.println();
}
}
// 关闭 Scanner
scanner.close();
}
// 大整数相减的函数
private static String subtractLargeNumbers(String num1, String num2) {
StringBuilder result = new StringBuilder();
int carry = 0;
// 补齐位数
while (num1.length() < num2.length()) {
num1 = "0" + num1;
}
while (num2.length() < num1.length()) {
num2 = "0" + num2;
}
// 从低位到高位逐位相减
for (int i = num1.length() - 1; i >= 0; i--) {
int digit1 = Character.getNumericValue(num1.charAt(i));
int digit2 = Character.getNumericValue(num2.charAt(i));
int tempResult = digit1 - digit2 - carry;
if (tempResult < 0) {
tempResult += 10;
carry = 1;
} else {
carry = 0;
}
result.insert(0, tempResult);
}
// 移除前导零
while (result.length() > 1 && result.charAt(0) == '0') {
result.deleteCharAt(0);
}
return result.toString();
}
}
此代码定义了一个 subtractLargeNumbers
函数,用于实现大整数相减。在主程序中,它通过读取输入、调用相应的函数并输出结果,模拟了大整数的减法。这种方法适用于手动模拟大整数运算,但相比 BigInteger
来说,实现起来更为复杂。