您的位置:首页 > 编程语言 > Java开发

杭电ACM大数JAVA提交实例

2012-03-22 20:34 393 查看
(提交时需将类名改为Main)

1002A+BProblemII

ProblemDescription
Ihaveaverysimpleproblemforyou.GiventwointegersAandB,yourjobistocalculatetheSumofA+B.
Input
ThefirstlineoftheinputcontainsanintegerT(1<=T<=20)whichmeansthenumberoftestcases.ThenTlinesfollow,eachlineconsistsoftwopositiveintegers,AandB.Noticethattheintegersareverylarge,thatmeansyoushouldnotprocess
thembyusing32-bitinteger.Youmayassumethelengthofeachintegerwillnotexceed1000.
Output
Foreachtestcase,youshouldoutputtwolines.Thefirstlineis"Case#:",#meansthenumberofthetestcase.Thesecondlineistheanequation"A+B=Sum",SummeanstheresultofA+B.Notetherearesomespacesinttheequation.
Outputablanklinebetweentwotestcases.
SampleInput
2
12
112233445566778899998877665544332211
SampleOutput
Case1:
1+2=3

Case2:
112233445566778899+998877665544332211=1111111111111111110
importjava.math.BigInteger;

importjava.util.Scanner;


publicclassMain{

publicstaticvoidmain(Stringargs[]){

BigIntegera=BigInteger.valueOf(0),b=BigInteger.valueOf(0);

intc;

Scannercin=newScanner(System.in);

booleanflag=false;

while(cin.hasNextInt()){

c=cin.nextInt();

for(inti=1;i<=c;i++){

a=cin.nextBigInteger();

b=cin.nextBigInteger();

if(flag){

System.out.println();

}

flag=true;

System.out.println("Case"+i+":");

System.out.println(a+"+"+b+"="+a.add(b));

}

}

}

}


1042N!
ProblemDescription
GivenanintegerN(0≤N≤10000),yourtaskistocalculateN!
Input
OneNinoneline,processtotheendoffile.
Output
ForeachN,outputN!inoneline.
SampleInput
1
2
3
SampleOutput
1
2
6
importjava.math.BigInteger;
importjava.util.Scanner;

publicclassMain{
publicstaticvoidmain(String[]
args){
intn;
BigIntegermul;
Scannercin
=new
Scanner(System.
in);
while
(
cin.hasNextInt()){
n
=cin.nextInt();
mul
=BigInteger.ONE;
for
(int
i
=
1;i<=n;i++){
mul
=mul.multiply(BigInteger.valueOf((long)
i));
}
System.out.println(mul);
}
}
}

1047IntegerInquiry
ProblemDescription
OneofthefirstusersofBIT'snewsupercomputerwasChipDiller.Heextendedhisexplorationofpowersof3togofrom0to333andheexploredtakingvarioussumsofthosenumbers.

``Thissupercomputerisgreat,''remarkedChip.``IonlywishTimothywereheretoseetheseresults.''(Chipmovedtoanewapartment,onceonebecameavailableonthethirdflooroftheLemonSkyapartmentsonThirdStreet.)
Input
Theinputwillconsistofatmost100linesoftext,eachofwhichcontainsasingleVeryLongInteger.EachVeryLongIntegerwillbe100orfewercharactersinlength,andwillonlycontaindigits(noVeryLongIntegerwillbenegative).

Thefinalinputlinewillcontainasinglezeroonalinebyitself.
Output
YourprogramshouldoutputthesumoftheVeryLongIntegersgivenintheinput.

Thisproblemcontainsmultipletestcases!

ThefirstlineofamultipleinputisanintegerN,thenablanklinefollowedbyNinputblocks.Eachinputblockisintheformatindicatedintheproblemdescription.Thereisablanklinebetweeninputblocks.

TheoutputformatconsistsofNoutputblocks.Thereisablanklinebetweenoutputblocks.
SampleInput
1
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
SampleOutput
370370367037037036703703703670
importjava.math.BigInteger;

importjava.util.Scanner;


publicclassMain{

publicstaticvoidmain(String[]args){

intn;

BigIntegertmp,res;

booleanflag=false;

Scannercin=newScanner(System.in);

cin.hasNextInt();

n=cin.nextInt();

for(inti=0;i<n;i++){

if(flag)

System.out.println();

flag=true;

res=BigInteger.ZERO;

while(cin.hasNextBigInteger()){

tmp=cin.nextBigInteger();

if(tmp.intValue()==0)

break;

res=res.add(tmp);

}

System.out.println(res);

}

}

}

1063Exponentiation
ProblemDescription
Problemsinvolvingthecomputationofexactvaluesofverylargemagnitudeandprecisionarecommon.Forexample,thecomputationofthenationaldebtisataxingexperienceformanycomputersystems.

ThisproblemrequiresthatyouwriteaprogramtocomputetheexactvalueofRnwhereRisarealnumber(0.0<R<99.999)andnisanintegersuchthat0<n<=25.
Input
TheinputwillconsistofasetofpairsofvaluesforRandn.TheRvaluewilloccupycolumns1through6,andthenvaluewillbeincolumns8and9.
Output
TheoutputwillconsistofonelineforeachlineofinputgivingtheexactvalueofR^n.Leadingzerosshouldbesuppressedintheoutput.Insignificanttrailingzerosmustnotbeprinted.Don'tprintthedecimalpointiftheresultisaninteger.
SampleInput
95.12312
0.432120
5.123415
6.75929
98.99910
1.010012
SampleOutput
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

importjava.math.BigDecimal;
importjava.util.Scanner;
publicclassMain{
publicstaticvoidmain(Stringargs[])
{

BigDecimalr,mul;
intn;
Scannercin
=new
Scanner(System.
in);
while
(
cin.hasNextBigDecimal()){
r
=cin.nextBigDecimal();
mul
=BigDecimal.ONE;
n
=cin.nextInt();
for
(int
i
=
0;i<n;i++){
mul
=mul.multiply(r);
}
Stringstr
=mul.stripTrailingZeros().toPlainString();
if
(
str.charAt(0)==
'0')
System.out.println(str.substring(1));
else
System.out.println(str);
}
}
}

1316HowManyFibs?
ProblemDescription
RecallthedefinitionoftheFibonaccinumbers:

f1:=1

f2:=2

fn:=fn-1+fn-2(n>=3)

Giventwonumbersaandb,calculatehowmanyFibonaccinumbersareintherange[a,b].
Input
Theinputcontainsseveraltestcases.Eachtestcaseconsistsoftwonon-negativeintegernumbersaandb.Inputisterminatedbya=b=0.Otherwise,a<=b<=10^100.Thenumbersaandbaregivenwithnosuperfluousleadingzeros.
Output
ForeachtestcaseoutputonasinglelinethenumberofFibonaccinumbersfiwitha<=fi<=b.
SampleInput
10100
12345678909876543210
00
SampleOutput
5
4
importjava.math.BigInteger;

importjava.util.Scanner;


publicclassMain{

publicstaticvoidmain(Stringargs[]){

BigIntegercount;

BigIntegerf[]=newBigInteger[10005];

BigIntegera=BigInteger.valueOf(0),b=BigInteger.valueOf(0);

f[1]=BigInteger.valueOf(1);

f[2]=BigInteger.valueOf(2);

for(inti=3;i<10005;i++){

f[i]=f[i-1].add(f[i-2]);

}

Scannercin=newScanner(System.in);

while(cin.hasNextBigInteger()){

a=cin.nextBigInteger();

b=cin.nextBigInteger();

if(a.equals(BigInteger.valueOf(0))

&&b.equals(BigInteger.valueOf(0))){

break;

}else{

count=BigInteger.valueOf(0);

for(inti=1;i<10000;i++){

if(((a.compareTo(f[i])==-1)||(a.compareTo(f[i])==0))

&&((b.compareTo(f[i])==1)||(b.compareTo(f[i])==0))){

count=count.add(BigInteger.valueOf(1));

}

}

System.out.println(count);

}

}

}

}

1715大菲波数
ProblemDescription
Fibonacci数列,定义如下:

f(1)=f(2)=1

f(n)=f(n-1)+f(n-2)n>=3。

计算第n项Fibonacci数值。
Input
输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。
Output
输出为N行,每行为对应的f(Pi)。
SampleInput
5
1
2
3
4
5
SampleOutput
1
1
2
3
5
importjava.io.*;

importjava.util.*;

importjava.math.*;

publicclassMain{

publicstaticvoidmain(String[]args){

intn;

BigIntegerf[]=newBigInteger[1005];

f[1]=BigInteger.valueOf(1);

f[2]=BigInteger.valueOf(1);

for(inti=3;i<1005;i++){

f[i]=f[i-1].add(f[i-2]);

}

Scannercin=newScanner(System.in);

while(cin.hasNextInt()){

n=cin.nextInt();

intp;

for(inti=0;i<n;i++){

p=cin.nextInt();

System.out.println(f[p]);

}

}

}

}

1753大明A+B
ProblemDescription
话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。

这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。

现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。
Input
本题目包含多组测试数据,请处理到文件结束。

每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
Output
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见SampleOutput。
SampleInput
1.12.9
1.11111111112.3444323343
11.1
SampleOutput
4
3.4555434454
2.1
importjava.io.*;

importjava.util.*;

importjava.math.*;


publicclassMain{

publicstaticvoidmain(String[]args){

BigDecimala,b;

Scannercin=newScanner(System.in);

while(cin.hasNextBigDecimal()){

a=cin.nextBigDecimal();

b=cin.nextBigDecimal();

a=a.add(b);

Stringstr=a.stripTrailingZeros().toPlainString();

System.out.println(str);

}

}

[b][b][b]}[/b][/b][/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: