大数高精度之java处理
例题1 链接:点击打开链接 原题:
Octal Fractions
Description
Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example,0.75 in octal is 0.953125 (7/8 + 5/64) in decimal. All octal numbers of n digits to the right of the octal point can be expressed in no more than 3n decimal digits to the right of the decimal point.?
Write a program to convert octal numerals between 0 and 1,inclusive,into equivalent decimal numerals. Input
The input to your program will consist of octal numbers,one per line,to be converted. Each input number has the form 0.d1d2d3 ... dk,where the di are octal digits (0..7). There is no limit on k.
Output
Your output will consist of a sequence of lines of the form?
where the left side is the input (in octal),and the right hand side the decimal (base 10) equivalent. There must be no trailing zeros,i.e. Dm is not equal to 0. Sample Input 0.75 0.0001 0.01234567 Sample Output 0.75 [8] = 0.953125 [10] 0.0001 [8] = 0.000244140625 [10] 0.01234567 [8] = 0.020408093929290771484375 [10] Source
Southern African 2001
java.source:
import java.util.*; 例题2 链接:点击打开链接 原题:
Water Treatment Plants
Description
River polution control is a major challenge that authorities face in order to ensure future clean water supply. Sewage treatment plants are used to clean-up the dirty water comming from cities before being discharged into the river.?
As part of a coordinated plan,a pipeline is setup in order to connect cities to the sewage treatment plants distributed along the river. It is more efficient to have treatment plants running at maximum capacity and less-used ones switched off for a period. So,each city has its own treatment plant by the river and also a pipe to its neighbouring city upstream and a pipe to the next city downstream along the riverside. At each city's treatment plant there are three choices:?
The choices above ensure that:? every city must have its water treated somewhere and? at least one city must discharge the cleaned water into the river.? Let's represent a city discharging water into the river as "V" (a downwards flow),passing water onto its neighbours as ">" (to the next city on its right) or else "<" (to the left). When we have several cities along the river bank,we assign a symbol to each (V,< or >) and list the cities symbols in order. For example,two cities,A and B,can? each treat their own sewage and each discharges clean water into the river. So A's action is denoted V as is B's and we write "VV" ;? or else city A can send its sewage along the pipe (to the right) to B for treatment and discharge,denoted ">V";? or else city B can send its sewage to (the left to) A,which treats it with its own dirty water and discharges (V) the cleaned water into the river. So A discharges (V) and B passes water to the left (<),and we denote this situation as "V<".? We could not have "><" since this means A sends its water to B and B sends its own to A,so both are using the same pipe and this is not allowed. Similarly "<<" is not possible since A's "<" means it sends its water to a non-existent city on its left.? So we have just 3 possible set-ups that fit the conditions:? A B A > B A < B V V V V RIVER~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~RIVER "VV" ">V" "V<" If we now consider three cities,we can determine 8 possible set-ups.? Your task is to produce a program that given the number of cities NC (or treatment plants) in the river bank,determines the number of possible set-ups,NS,that can be made according to the rules define above.? You need to be careful with your design as the number of cities can be as large as 100.? Input
The input consists of a sequence of values,where each value represents the number of cities.
Output
Your output should be a sequence of values,where each value represents the number of possible set-ups for the corresponding number of cities read in the same input line.
Sample Input 2 3 20 Sample Output 3 8 102334155 Source
Southwestern Europe 2002
题意思路转自
http://blog.sina.com.cn/s/blog_6635898a0100ovby.html
题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择 问总共有多少种处理情况,即不同又符合实际的><V组合。 ? 思路:DP+高精度。DP部分,易得最右边城市的状态只可能用3种:>V,V,<。故分三种状态讨论,设dp[i][0]为第i个城市的状态为:> V ,dp[i][1]为:V ,dp[i][2]为:<。由实际流动的可能性可以得到状态转移方程: dp[i][0] = dp[i-1][0] + dp[i-1][1]; dp[i][1] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2]; dp[i][2] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2]; 然后可以整理为:dp[i] = 3 * dp[i-1] -dp[i-2]。然后用java的BigInteger预处理就OK了。 import java.util.*; 例题3 链接:点击打开链接 原题:
NUMBER BASE CONVERSION
Description
Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:?
{ 0-9,A-Z,a-z }? HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next,when you get back to the original base,you should get the original number.? Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10,B = 11,...,Z = 35,a = 36,b = 37,z = 61 (0-9 have their usual meanings).?
Output
The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank.?
Sample Input 8 62 2 abcdefghiz 10 16 1234567890123456789012345678901234567890 16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 23 333YMHOUE8JPLT7OX6K9FYCQ8A 23 49 946B9AA02MI37E3D3MMJ4G7BL2F05 49 61 1VbDkSIMJL3JjRgAdlUfcaWj 61 5 dl9MDSWqwHjDnToKcsWE1S 5 10 42104444441001414401221302402201233340311104212022133030 Sample Output 62 abcdefghiz 2 11011100000100010222220010010110022222001001100011010010001 10 1234567890123456789012345678901234567890 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 333YMHOUE8JPLT7OX6K9FYCQ8A 35 333YMHOUE8JPLT7OX6K9FYCQ8A 23 946B9AA02MI37E3D3MMJ4G7BL2F05 23 946B9AA02MI37E3D3MMJ4G7BL2F05 49 1VbDkSIMJL3JjRgAdlUfcaWj 49 1VbDkSIMJL3JjRgAdlUfcaWj 61 dl9MDSWqwHjDnToKcsWE1S 61 dl9MDSWqwHjDnToKcsWE1S 5 42104444441001414401221302402201233340311104212022133030 5 42104444441001414401221302402201233340311104212022133030 10 1234567890123456789012345678901234567890 Source
Greater New York 2002
题意:大数的进制转换。将a进制的正整数num转换为b进制的正整数。
思路:高精度,java,要注意为num = 0的情况。 题意思路注释转自:http://blog.sina.com.cn/s/blog_6635898a0100ovel.htmlMy java.source: import java.io.*; 例题4 链接:点击打开链接 原题:
Heritage
Description
Your rich uncle died recently,and the heritage needs to be divided among your relatives and the church (your uncle insisted in his will that the church must get something). There are N relatives (N <= 18) that were mentioned in the will. They are sorted in descending order according to their importance (the first one is the most important). Since you are the computer scientist in the family,your relatives asked you to help them. They need help,because there are some blanks in the will left to be filled. Here is how the will looks:?
Relative #1 will get 1 / ... of the whole heritage,? Relative #2 will get 1 / ... of the whole heritage,? ---------------------- ...? Relative #n will get 1 / ... of the whole heritage.? The logical desire of the relatives is to fill the blanks in such way that the uncle's will is preserved (i.e the fractions are non-ascending and the church gets something) and the amount of heritage left for the church is minimized. Input
The only line of input contains the single integer N (1 <= N <= 18).
Output
Output the numbers that the blanks need to be filled (on separate lines),so that the heritage left for the church is minimized.
Sample Input 2 Sample Output 2 3 Source
ural 1108
题意:将一份遗产分成n份,1/X1,1/X2,1/X3,1/Xn。 使得X1 >= X2 >= X3 >= ... >= Xn,都为正整数。最后剩下的分给教堂,让其分得的财产最少,但不能没有。 ? 思路:贪心+高精度。首先由贪心的思想可以知道,如果剩下1/6的遗产给最后一人和教堂,易得最后一人的得到的遗产为1/7。由这个思想可以列出 1/(Xn-1) = 1 - 1/X1 - 1/X2 -...- 1/X(n-1)。然后这个式子和X(n+1)的式子相消,化简,可得X(n+1) = Xn * (Xn - 1) + 1。然后就用java的大数A掉。 题意思路注释转自: http://blog.sina.com.cn/s/blog_6635898a0100ovom.htmlMy java source: import java.util.*; 例题5 链接:点击打开链接 原题:
Integer Inquiry
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 Source
East Central North America 1996
My java.source: import java.util.*; 例题6 链接:点击打开链接 原题:
Just the Facts
Description
The expression N!,read as "N factorial," denotes the product of the first N positive integers,where N is nonnegative. So,for example,?
N N! 0 1 1 1 2 2 3 6 4 24 5 120 10 3628800 For this problem,you are to write a program that can compute the last non-zero digit of any factorial for (0 <= N <= 10000). For example,if your program is asked to compute the last nonzero digit of 5!,your program should produce "2" because 5! = 120,and 2 is the last nonzero digit of 120. Input
Input to the program is a series of nonnegative integers not exceeding 10000,each on its own line with no other letters,digits or spaces. For each integer N,you should read the value and compute the last nonzero digit of N!.
Output
For each integer input,the program should print exactly one line of output. Each line of output should contain the value N,right-justified in columns 1 through 5 with leading blanks,not leading zeroes. Columns 6 - 9 must contain " -> " (space hyphen greater space). Column 10 must contain the single last non-zero digit of N!.
Sample Input 1 2 26 125 3125 9999 Sample Output 1 -> 1 2 -> 2 26 -> 4 125 -> 8 3125 -> 2 9999 -> 8 Source
South Central USA 1997
java.source: import java.io.*; import java.util.*; import java.math.*; import java.text.*; public class Main { public static void main(String args[]) { BigInteger n,nn; String s; int i; Scanner cin=new Scanner(System.in); while(cin.hasNext()) { n=cin.nextBigInteger(); nn=BigInteger.valueOf(1); for(i=2;i<=n.intValue();i++) { nn=nn.multiply(BigInteger.valueOf(i)); } s=nn.toString(); for(i=s.length()-1;i>=0;i--) { if(s.charAt(i)!='0') { break; } } System.out.printf("%5d -> %cn",s.charAt(i)); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |