杭電OJ的輸入輸出格式題_第1頁
杭電OJ的輸入輸出格式題_第2頁
杭電OJ的輸入輸出格式題_第3頁
杭電OJ的輸入輸出格式題_第4頁
杭電OJ的輸入輸出格式題_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

杭電OJ的輸入/輸出格式題輸入一、1000A題目名稱:A+BProblem鏈接地址:00TimeLimit:1Seconds

MemoryLimit:32768KTimeLimit:2000/1000MS(Java/Others)

MemoryLimit:65536/32768K(Java/Others)ProblemDescriptionCalculateA+B.InputEachlinewillcontaintwointegersAandB.Processtoendoffile.OutputForeachcase,outputA+Binoneline.SampleInput11SampleOutput2參考答案#include<stdio.h>intmain(void){ inta,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",a+b);return0;}二、1002題目名稱:A+BProblemII鏈接地址:h02TimeLimit:1Seconds

MemoryLimit:32768KTime

Limit:

2000/1000

MS

(Java/Others)

Memory

Limit:

65536/32768

K

(Java/Others)Total

Submission(s):

22627

Accepted

Submission(s):

4035Problem

DescriptionI

have

a

very

simple

problem

for

you.

Given

two

integers

A

and

B,

your

job

is

to

calculate

the

Sum

of

A

+

B.InputThe

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.OutputFor

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

Input21

2Sample

OutputCase

1:1

+

2

=

3Case

2:思路:此題是手工模擬兩個大數(shù)相加的問題。兩個輸入的數(shù)字用字符數(shù)組存儲。不超過1000位,也就是說用整型數(shù)定義是不行的,然后想到的就是用整型數(shù)組存儲和的各位數(shù)。具體解題思路如下:

1、先定義兩個字符型數(shù)組str1和str2,用以保存輸入的數(shù)字,然后再定義一個整型數(shù)組,保存兩加數(shù)的和的各位。

2、下面來求和計(jì)算,先將字符型數(shù)組str1中的各位從后面倒著取出,轉(zhuǎn)換成數(shù)字存儲在整型數(shù)組a中〔即按相反的順序進(jìn)行存放〕,然后,再將字符型數(shù)組str2中的各位從后面倒著取出,轉(zhuǎn)換成數(shù)字,然后與a數(shù)組中的各位相加,并參加進(jìn)位,這樣相當(dāng)于將兩個數(shù)從最低位到最高位進(jìn)行相加,但是數(shù)組a中存放的和是從最低位到最高位,所以輸出時要注意。3、最后就是格式控制好輸出。參考答案#include<stdio.h>#include<string.h>#include<stdlib.h>intmain(){charstr1[1005],str2[1005];intn,count=0,i,j,flag;inta[1005];scanf("%d",&n);while(n--){scanf("%s%s",str1,str2);memset(a,0,sizeof(a));for(i=strlen(str1)-1,j=0;i>=0;i--,j++)a[j]=str1[i]-'0';for(i=strlen(str2)-1,j=0;i>=0;i--,j++){a[j]=a[j]+str2[i]-'0';a[j+1]=a[j+1]+a[j]/10;a[j]=a[j]%10;}count++;printf("Case%d:\n",count);printf("%s+%s=",str1,str2);flag=0;for(i=1004;i>=0;i--)if(flag||a[i]){printf("%d",a[i]);flag=1;}printf("\n");if(n!=0)printf("\n");}return0;}三、1089題目名稱:A+BforInput-OutputPractice(I)鏈接地址:TimeLimit:1Seconds

MemoryLimit:32768K1089A+BforInput-OutputPractice(I)ProblemDescriptionYourtaskistoCalculatea+b.Tooeasy?!Ofcourse!Ispeciallydesignedtheproblemforacmbeginners.Youmusthavefoundthatsomeproblemshavethesametitleswiththisone,yes,alltheseproblemsweredesignedforthesameaim.InputTheinputwillconsistofaseriesofpairsofintegersaandb,separatedbyaspace,onepairofintegersperline.OutputForeachpairofinputintegersaandbyoushouldoutputthesumofaandbinoneline,andwithonelineofoutputforeachlineininput.SampleInput151020SampleOutput630參考答案#include<stdio.h>intmain(void){ inta,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",a+b);return0;}四、1090A題目名稱:A+BforInput-OutputPractice(II)鏈接地址:90TimeLimit:1Seconds

MemoryLimit:32768K輸入一開始就會說有n個InputBlock,下面接著是輸入n個InputBlock。參見:HDOJ_1090〔=1090〕。1090A+BforInput-OutputPractice(II)ProblemDescriptionYourtaskistocalculatea+b.InputInputcontainsanintegerNinthefirstline,andthenNlinesfollow.Eachlineconsistsofapairofintegersaandb,separatedbyaspace,onepairofintegersperline.OutputForeachpairofinputintegersaandbyoushouldoutputthesumofaandbinoneline,andwithonelineofoutputforeachlineininput.Sampleinput2151020Sampleoutput630參考答案#include<stdio.h>intmain(){intn,i,a,b;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d",&a,&b);printf("%d\n",a+b);}return0;}五、1091A題目名稱:A+BforInput-OutputPractice(III)鏈接地址:91TimeLimit:1Seconds

MemoryLimit:32768K輸入不說明有多少個InputBlock,但以某個特殊輸入為結(jié)束標(biāo)志。參見:HDOJ_1091〔=1091〕。1091A+BforInput-OutputPractice(III)ProblemDescriptionYourtaskistocalculatea+b.InputInputcontainsmultipletestcases.Eachtestcasecontainsapairofintegersaandb,onepairofintegersperline.Atestcasecontaining00terminatestheinputandthistestcaseisnottobeprocessed.OutputForeachpairofinputintegersaandbyoushouldoutputthesumofaandbinoneline,andwithonelineofoutputforeachlineininput.Sampleinput15102000Sampleoutput630參考答案#include<stdio.h>intmain(){ inta,b;while(scanf("%d%d",&a,&b)!=EOF&&(a!=0||b!=0))printf("%d\n",a+b);return0;}或#include<stdio.h>intmain(){ inta,b;while(scanf("%d%d",&a,&b)!=EOF)if(a==0&&b==0)break;elseprintf("%d\n",a+b);return0;}六、1092A題目名稱:A+BforInput-OutputPractice(IV)鏈接地址:n/showproblem.php?pid=1092TimeLimit:1Seconds

MemoryLimit:32768K以上幾種情況的組合,可以參照如下網(wǎng)頁。參見:HDOJ_1092〔=1092〕。1092A+BforInput-OutputPractice(IV)ProblemDescriptionYourtaskistoCalculatethesumofsomeintegers.InputInputcontainsmultipletestcases.EachtestcasecontainsaintegerN,andthenNintegersfollowinthesameline.Atestcasestartingwith0terminatestheinputandthistestcaseisnottobeprocessed.OutputForeachgroupofinputintegersyoushouldoutputtheirsuminoneline,andwithonelineofoutputforeachlineininput.SampleInput412345123450SampleOutput1015參考答案#include<stdio.h>intmain(){intn,sum,i,t;while(scanf("%d",&n)!=EOF&&n!=0){sum=0;for(i=0;i<n;i++){scanf("%d",&t);sum=sum+t;}printf("%d\n",sum);}}七、1093A題目名稱:A+BforInput-OutputPractice(V)鏈接地址:93TimeLimit:1Seconds

MemoryLimit:32768K以上幾種情況的組合,可以參照如下網(wǎng)頁。參見:HDOJ_1092〔=1093〕。1093ProblemDescriptionYourtaskistocalculatethesumofsomeintegers.InputInputcontainsanintegerNinthefirstline,andthenNlinesfollow.EachlinestartswithaintegerM,andthenMintegersfollowinthesameline.OutputForeachgroupofinputintegersyoushouldoutputtheirsuminoneline,andwithonelineofoutputforeachlineininput.SampleInput241234512345SampleOutput1015參考答案#include<stdio.h>intmain(){intn,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=EOF){for(i=0;i<n;i++){scanf("%d",&b);for(j=0;j<b;j++){scanf("%d",&a);sum+=a;}printf("%d\n",sum);sum=0;}}return0;}八、1094A題目名稱:A+BforInput-OutputPractice(VI)鏈接地址:94TimeLimit:1Seconds

MemoryLimit:32768K以上幾種情況的組合,可以參照如下網(wǎng)頁。參見:HDOJ_1092〔owproblem.php?pid=1094〕。1094AProblemDescriptionYourtaskistocalculatethesumofsomeintegers.InputInputcontainsmultipletestcases,andonecaseoneline.EachcasestartswithanintegerN,andthenNintegersfollowinthesameline.OutputForeachtestcaseyoushouldoutputthesumofNintegersinoneline,andwithonelineofoutputforeachlineininput.SampleInput41234512345SampleOutput1015參考答案#include<stdio.h>intmain(){intn,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=EOF){for(j=0;j<n;j++){scanf("%d",&a);sum+=a;}printf("%d\n",sum);sum=0;}return0;}輸出1、第一類輸出一個InputBlock對應(yīng)一個OutputBlock,OutputBlock之間沒有空行。參見:HDOJ_1089〔:///showproblem.php?pid=1089〕。2、第二類輸出一個InputBlock對應(yīng)一個OutputBlock,每個OutputBlock之后都有空行。參見:HDOJ_1095〔=1095〕。1095ProblemDescriptionYourtaskistoCalculatea+b.InputTheinputwillconsistofaseriesofpairsofintegersaandb,separatedbyaspace,onepairofintegersperline.OutputForeachpairofinputintegersaandbyoushouldoutputthesumofaandb,andfollowedbyablankline.SampleInput151020SampleOutput630參考答案#include<stdio.h>intmain(){inta,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n\n",a+b);}3、第三類輸出一個InputBlock對應(yīng)一個OutputBlock,OutputBlock之間有空行。參見:HDOJ_1096〔=1096〕。1096ProblemDescriptionYourtaskistocalculatethesumofsomeintegers.InputInputcontainsanintegerNinthefirstline,andthenNlinesfollow.EachlinestartswithaintegerM,andthenMintegersfollowinthesameline.OutputForeachgroupofinputintegersyoushouldoutputtheirsuminoneline,andyoumustnotethatthereisablanklinebetweenoutputs.SampleInput3412345123453123SampleOutput10156參考答案#include<stdio.h>intmain(){inticase,n,i,j,a,sum;scanf("%d",&icase);for(i=0;i<icase;i++){sum=0;scanf("%d",&n);for(j=0;j<n;j++){scanf("%d",&a);sum+=a;}if(i<icase-1)printf("%d\n\n",sum);elseprintf("%d\n",sum);}}1229還是A+B參見:HDOJ_1229〔=1229〕。TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others),TotalSubmission(s):15959AcceptedSubmission(s):7765ProblemDescription讀入兩個小于10000的正整數(shù)A和B,計(jì)算A+B。需要注意的是:如果A和B的末尾K〔不超過8〕位數(shù)字相同,請直接輸出-1。Input測試輸入包含假設(shè)干測試用例,每個測試用例占一行,格式為"ABK",相鄰兩數(shù)字有一個空格間隔。當(dāng)A和B同時為0時輸入結(jié)束,相應(yīng)的結(jié)果不要輸出。Output對每個測試用例輸出1行,即A+B的值或者是-1。SampleInput121112111088236643001SampleOutput3-1-1100參考答案#include<stdio.h>#include<math.h>intmain(){inta,b,n,i,sum;while(scanf("%d%d%d",&a,&b,&n)==3){if(a==0&&b==0)break;sum=(int)pow(10,n);if(a%sum==b%sum)printf("-1\n");elseprintf("%d\n",a+b);}return0;}說明:上面的sum=(int)pow(10,n);實(shí)際上是求10有n次方,也可以用下面的循環(huán)來求得:sum=1;for(i=1;i<=n;i++){sum*=10;}1106排序參見:HDOJ_1106〔=1106〕。TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others),TotalSubmission(s):15959AcceptedSubmission(s):7765ProblemDescription輸入一行數(shù)字,如果我們把這行數(shù)字中的‘5’都看成空格,那么就得到一行用空格分割的假設(shè)干非負(fù)整數(shù)〔可能有些整數(shù)以‘0’開頭,這些頭部的‘0’應(yīng)該被忽略掉,除非這個整數(shù)就是由假設(shè)干個‘0’組成的,這時這個整數(shù)就是0〕。你的任務(wù)是:對這些分割得到的整數(shù),依從小到大的順序排序輸出。Input輸入包含多組測試用例,每組輸入數(shù)據(jù)只有一行數(shù)字〔數(shù)字之間沒有空格〕,這行數(shù)字的長度不大于1000。

輸入數(shù)據(jù)保證:分割得到的非負(fù)整數(shù)不會大于100000000;輸入數(shù)據(jù)不可能全由‘5’組成。Output對于每個測試用例,輸出分割得到的整數(shù)排序的結(jié)果,相鄰的兩個整數(shù)之間用一個空格分開,每組輸出占一行。SampleInputSampleOutput07712312320參考答案#include<cstdio>//cstdio就是將stdio.h的內(nèi)容用C++的頭文件形式表現(xiàn)出來#include<cstring>#include<algorithm>//需要用到C++中對數(shù)組進(jìn)行升冪排序sort,需要包含頭文件<algorithm>usingnamespacestd;intmain(){charstr[1100];inti,k,len,sum,ws;//ws存儲子字符串位數(shù),k存儲已得到的整數(shù)個數(shù)inta[1100];//把分割后的數(shù)值存入數(shù)組a中while(scanf("%s",str)!=EOF){len=strlen(str);sum=0;ws=0;k=0;//ws存儲子字符串位數(shù),k存儲已得到的整數(shù)個數(shù)for(i=0;i<=len;i++){if(str[i]=='5'||str[i]=='\0')//意味著一個數(shù)結(jié)束,把數(shù)存入到a[k]{if(ws>0)a[k++]=sum;ws=0;sum=0;//為計(jì)算下一個數(shù)做準(zhǔn)備}else//str[i]!=5且str[i]!='\0',即str[i]為有效數(shù)字{sum=sum*10+str[i]-'0';ws++;}}sort(a,a+k);//調(diào)用C++中的sort排序方法printf("%d",a[0]);//a[0]需要分開打印,因?yàn)樗懊鏌o空格for(i=1;i<k;i++)printf("%d",a[i]);printf("\n");}return0;}1003MaxSum參見:HDOJ_1003〔=1003〕。TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):53856AcceptedSubmission(s):12109ProblemDescriptionGivenasequencea[1],a[2],a[3]......a[n],yourjobistocalculatethemaxsumofasub-sequence.Forexample,given(6,-1,5,4,-7),themaxsuminthissequenceis6+(-1)+5+4=14.InputThefirstlineoftheinputcontainsanintegerT(1<=T<=20)whichmeansthenumberoftestcases.ThenTlinesfollow,eachlinestartswithanumberN(1<=N<=100000),thenNintegersfollowed(alltheintegersarebetween-1000and1000).OutputForeachtestcase,youshouldoutputtwolines.Thefirstlineis"Case#:",#meansthenumberofthetestcase.Thesecondlinecontainsthreeintegers,theMaxSuminthesequence,thestartpositionofthesub-sequence,theendpositionofthesub-sequence.Iftherearemorethanoneresult,outputthefirstone.Outputablanklinebetweentwocases.SampleInput256-154-7706-11-67-5SampleOutputCase1:1414Case2:716參考程序:#include<stdio.h>intmain(){//t為輸入測試樣例的組數(shù),n為每個測試樣例的數(shù)據(jù)元素的個數(shù)/begin用來暫時存儲b保存的和的起始位置,step為測試樣例的個數(shù) intn,t,b,begin,step=1,i,now;///maxsum保存之前算出來的最大和,b存儲目前在讀入數(shù)據(jù)前保存的和,now為每次輸入數(shù)據(jù) intbesti,bestj,maxsum; scanf("%d",&t);while(t--) {scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&now); if(i==1)//初始化{maxsum=b=now;//maxsum保存之前算出來的最大和,b存儲目前在讀入數(shù)據(jù)前保存的和,a[i]為讀入數(shù)據(jù)begin=besti=bestj=1;//begin用來暫時存儲b保存的和的起始位置,當(dāng)b>maxsum時將begin賦在besti位置,besti,bestj保存最大和的開始和結(jié)束位置}else{if(now>b+now)//如果之前存儲的和b加上現(xiàn)在的數(shù)據(jù)now比現(xiàn)在的數(shù)據(jù)小,就把存儲的和換成現(xiàn)在的數(shù)據(jù),{b=now;begin=i;//預(yù)存的位置要重置}elseb+=now;//反之就說明數(shù)據(jù)在遞增,可以直接加上}if(b>maxsum)//跟之前算出來的最大和進(jìn)行比擬,如果大于,位置和數(shù)據(jù)就要重置{maxsum=b;besti=begin;bestj=i;}}printf("Case%d:\n%d%d%d\n",step++,maxsum,besti,bestj);if(t)printf("\n"); } return0;}或者#include<stdio.h>#defineMin-999999intmain(){intdata[100000],start,end,i;//start,end存放最大子段和的起始和結(jié)束的位置intm,n;//存儲測試樣例的總數(shù)目,n為每個測試樣例的數(shù)據(jù)元素個數(shù)intstep=1;//存放對當(dāng)前測試的個數(shù)intmax;//max存儲最大子段和intk,sum;//k用來暫時存儲sum保存的和的起始位置scanf("%d",&m);while(m--){scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&data[i]);max=Min;k=1;sum=0;for(i=1;i<=n;i++){

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論