Java 解决一些ACM中大数问题
运算 | 结果的首选标度 |
---|---|
加 | max(addend.scale(), augend.scale()) |
减 | max(minuend.scale(), subtrahend.scale()) |
乘 | multiplier.scale() + multiplicand.scale() |
除 | dividend.scale() - divisor.scale() |
Problem D: Integer Inquiry
Time Limit:Submit:
[Submit][Status][Web Board]
Description
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)
Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
HINT
大数相加,遇到输入0时输出结果,直到读到EOF(文件结尾)为止
代码:
import java.io.FileInputStream; import java.io.PrintStream; import java.io.FileOutputStream; public class IntegerInquiry { public static void main(String[ ] args) throws Exception{ FileInputStream in =new FileInputStream("in.txt"); System.setIn(in); Scanner input = new Scanner(System.in); PrintStream out = new PrintStream(new FileOutputStream("out.txt")); System.setOut(out); BigInteger bigInteger = input.nextBigInteger(); BigInteger sum = new BigInteger("0"); System.out.println(sum); while(input.hasNext()){ bigInteger=input.nextBigInteger(); if(!(bigInteger.toString()==("0"))) sum=sum.add(bigInteger); if(bigInteger.toString()==("0")){ System.out.println(sum); sum=new BigInteger("0"); } } } }
Problem E: Product
Time Limit:Submit:
[Submit][Status][Web Board]
Description
The problem is to multiply two integers X, Y. (0<=X,Y<10250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444
HINT
两个大数相乘,直到读到EOF(文件结尾)为止
代码:
import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.PrintStream; import java.math.BigInteger; public class IntegerMultiply { public static void main(String[] args)throws Exception{ FileInputStream in = new FileInputStream("in"); System.setIn(in); Scanner input =new Scanner(System.in); PrintStream out = new PrintStream(new FileOutputStream("out")); System.setOut(out); BigInteger a,b = new BigInteger("0"); while(input.hasNext()){ a = input.nextBigInteger(); b = input.nextBigInteger(); b = b.multiply(a); System.out.println(b); } } }
Problem F: Exponentiation
Time Limit:Submit:
[Submit][Status][Web Board]
Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999) and n is an integer such that $0 < n \le 25$.
Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967 221869852721 .000000051485546410769561 219945112767671548384817 602007263512038354297630 13462401 43992025569.928573701266488041146654 993318703707511666295476 720493953024 29448126.764121021618164430206909 037173276672 90429072743629540498.107596019456651774561044 010001 1.126825030131969720661201
HINT
大数a的n次幂,直到读到EOF(文件结尾)为止,其中忽略小数后面的0
代码:
import java.util.*; import java.io.*; import java.math.*; public class BigIntegrExponentiation { public static void main(String[] args)throws Exception{ FileInputStream in = new FileInputStream("input.txt"); System.setIn(in); Scanner input = new Scanner(System.in); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); BigDecimal a = new BigDecimal("0"); int b = 0; String c; while(input.hasNext()){ a = input.nextBigDecimal(); b = input.nextInt(); a = a.pow(b).stripTrailingZeros(); c = a.toPlainString(); if(c.charAt(0) == '0') c = c.substring(1); System.out.println(c); } } }
Problem G: If We Were a Child Again
Time Limit:Submit:
[ Submit][ Status][ Web Board]
Description
The Problem
The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.
But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people.
Input
Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).
Output
A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.
Sample Input
110 / 100
99 % 10
2147483647 / 2147483647
2147483646 % 2147483647
Sample Output
1
9
1
2147483646
HINT
大数求余与整除运算
代码:
import java.util.*; import java.io.*; import java.math.*; public class BigDecimalModAndDivide { public static void main(String[] args)throws Exception{ FileInputStream in = new FileInputStream("input1.txt"); System.setIn(in); Scanner input = new Scanner(System.in); PrintStream out = new PrintStream(new FileOutputStream("output1.txt")); System.setOut(out); BigInteger a,b,t =new BigInteger("1"); String c; while(input.hasNext()){ a = input.nextBigInteger(); c = input.next(); b = input.nextBigInteger(); if(c.equals("%")) t = a.mod(b); if(c.equals("/")) t = a .divide(b); System.out.println(t); } } }