[解題報告]《演算法競賽入門經典》基礎題Volume 0. Getting Started Uva10055 Uva10071 Uva10300 Uva458 Uva494 Uva490 Uva445

來源:互聯網
上載者:User
我愛A水題...嘿嘿...

 

UVA10055 - Hashmat the Brave Warrior

入門題,注意資料類型為longlong即可


ude <iostream><br />#include <cstdio><br />using namespace std;<br />int main(){<br />long long a,b;<br />while(scanf("%lld%lld",&a,&b)!=EOF){<br />printf("%lld/n",b-a>0?b-a:a-b);<br />}<br />return 0;<br />}  

 


UVA10071 - Back to High School Physics

入門題

#include <iostream><br />#include <cstdio><br />using namespace std;<br />int main(){<br />int a,b;<br />while(cin>>a>>b){<br />cout<<a*b*2<<endl;<br />}<br />return 0;<br />} 


10300 - Ecological Premium


題目沒仔細看,就是把每一行第一個和第三個數的積求出來即可

#include <iostream><br />#include <cstdio><br />using namespace std;<br />int main(){<br />int nCase;<br />cin>>nCase;<br />while(nCase-->0){<br />int a,b,c,sum=0,i1;<br />cin>>i1;<br />while(i1-->0){<br />cin>>a>>b>>c;<br />sum+=a*c;<br />}<br />cout<<sum<<endl;<br />}<br />return 0;<br />} 

 

 

UVA458 - The Decoder

入門題,每個字元等差變換

 

 

#include <iostream><br />#include <cstdio><br />#include <string><br />using namespace std;<br />int main(){<br />string str;<br />while(cin>>str){<br />for(int i=0;i<str.length();i++){<br />str[i]+='*'-'1';<br />}<br />cout<<str<<endl;<br />}<br />return 0;<br />} 

 

 

UVA494 - Kindergarten Counting Game

 

數單詞,找出有多少個連續字母串即可

#include <iostream><br />#include <cstdio><br />#include <string><br />using namespace std;<br />int main(){<br />string str;<br />while(getline(cin,str)){<br />int count=0;<br />for(int i=0;i<str.length();i++){<br />while(i<str.length()&&!isalpha(str[i])){<br />i++;<br />}<br />if(isalpha(str[i]))count++;<br />while(i<str.length()&&isalpha(str[i])){<br />i++;<br />}</p><p>}<br />cout<<count<<endl;<br />}<br />return 0;<br />} 

 

 

UVA490 - Rotating Sentences

 

有點類似求矩陣的倒置,先找出最長的串作為矩陣高,注意不夠串長的地方輸出空格

#include <iostream><br />#include <cstdio><br />using namespace std;<br />int main(){<br />string str[120];<br />int len=0;<br />int i=0,max=0,count=0;<br />while(getline(cin,str[i])){<br />len=str[i].length();<br /> if(len>max)max=len;<br />i++;<br />}<br />count=i;<br />for(int i=0;i<max;i++){<br />for(int j=count-1;j>=0;j--){<br />cout<<(i<str[j].length()?str[j][i]:' ');<br />}<br />cout<<endl;<br />}<br />return 0;<br />} 

 

 

UVA445 - Marvelous Mazes

每個字元輸出的個數與它前面的數字有關.數字都看做一位元,求和即可,注意b是空格.遇到!換行.

 

#include <iostream><br /> #include <cstdio><br /> using namespace std;<br /> int main(){<br />string str;<br />while(getline(cin,str)){<br />for(int i=0;i<str.length();i++){<br />int co=0;<br />while(str[i]>='0'&&str[i]<='9'){<br />co+=str[i]-'0';<br />i++;<br />}<br />while(co-->0){<br />cout<<(str[i]=='b'?' ':str[i]);<br />}<br />if(str[i]=='!'){<br />cout<<endl;<br />}<br />}<br />cout<<endl;<br />}<br />return 0;<br /> } 

 

 

UVA488 - Triangle Wave

用迴圈結構列印字串的題目..注意對空行的處理..煞是噁心..

#include <iostream><br />#include <cstdio><br />using namespace std;<br />int main(){<br />int nCase;<br />cin>>nCase;<br />while(nCase-->0){<br />int a,b;<br />cin>>a>>b;<br />for(int i=0;i<b;i++){<br />for(int j=1;j<=a;j++){<br />for(int k=0;k<j;k++){<br />cout<<j;<br />}<br />cout<<endl;<br />}<br />for(int j=a-1;j>=1;j--){<br />for(int k=0;k<j;k++){<br />cout<<j;<br />}<br />cout<<endl;<br />}<br />if(i!=b-1)cout<<endl;<br />}<br />if(nCase)cout<<endl;<br />}<br />return 0;<br />} 

 

 

UVA489 - Hangman Judge

貌似以前還玩過這個遊戲,錯了七次就輸了,再錯七次前猜對就贏了,還沒錯七次結束就是膽怯了

設一個標誌數組,對應正確答案的每個字元,為1時說明該位置猜對了

對所猜字串依次判斷,猜對則設標誌數組相應位置為1,並累積正確字元個數,如果不正確,錯誤次數加一

如果錯七次或者全部猜對離開遊戲,如果退出時沒有輸贏,則是膽怯了

#include <iostream><br />#include <cstdio><br />#include <string.h><br />using namespace std;<br />int main(){<br />int nCase;<br />while(cin>>nCase){<br />if(nCase==-1)break;<br />string answer,guess;<br />cin>>answer>>guess;<br />int correct=0;<br />int wrong=0;<br />int word[100];<br />memset(word,0,sizeof(word));<br />int isOver=0;<br />for(int i=0;i<guess.length();i++){<br />int isWrong=1;<br />for(int j=0;j<answer.length();j++){<br />if(guess[i]==answer[j]&&word[j]==0){word[j]=1;correct++;isWrong=0;}<br />}<br />if(isWrong==1)wrong++;<br />if(wrong==7){<br />cout<<"Round "<<nCase<<"/nYou lose./n";<br />isOver=1;<br />break;<br />}<br />if(correct==answer.length()){<br />cout<<"Round "<<nCase<<"/nYou win./n";<br />isOver=1;<br />break;<br />}<br />}<br />if(isOver==0){<br />cout<<"Round "<<nCase<<"/nYou chickened out./n";<br />}<br />}<br />return 0;<br />} 

 

 

 

UVA694 - The Collatz Sequence

3n+1問題..偶數除2,奇數3n+1,直到為1或者超出limit為止

 

 

#include <iostream><br />using namespace std;<br />int main(){<br />long long a,limit,b;<br />int nCase=1;<br />while(cin>>a>>limit){<br />b=a;<br />if(a==-1&&limit==-1)break;<br />int total=0;<br />while(1){<br />if(a==1){<br />total++;<br />break;<br />}<br />if(a>limit){<br />break;<br />}<br />if(a%2==0)a=a/2;<br />else a=a*3+1;<br />total++;<br />}<br /> cout<<"Case "<<nCase++<<": A = "<<b<<", limit = "<<limit<<", number of terms = "<<total<<endl;<br />}<br />return 0;<br />}  

 

 

 

 

UVA457 - Linear Cellular Automata

看了好久才看懂題目..E文不好的悲劇啊..

就是有40個培養皿,裡面有細菌,培養皿共有4個狀態,分別為0,1,2,3

初始第20個為1,其餘為0

第二天,培養皿的狀態取決於它本身以及周邊兩個培養皿的和,假設和為s

題目要求輸入一個數組DNA[10],值為0~3

那麼第二天該培養皿的狀態就為DNA[s]

一直迴圈,直到50為止,中間用以個臨時數組儲存前一天的狀態

#include <iostream><br />#include <cstdio><br /> using namespace std;<br /> int main(){<br /> int nCase;<br /> cin>>nCase;<br />while(nCase-->0){<br /> int DNA[11];<br /> int l=0;<br /> while(l<10){<br /> scanf("%d",&DNA[l]);<br /> l++;<br /> }<br /> int dish[43];<br /> int t_dish[43];<br /> for(int i=0;i<=41;i++){<br /> dish[i]=(i==20?1:0);<br /> t_dish[i]=dish[i];<br /> }<br /> l=0;<br /> while(l++<50){<br /> for(int i=1;i<=40;i++){<br /> t_dish[i]=dish[i];<br /> switch(dish[i]){<br /> case 0:printf(" ");break;<br /> case 1:printf(".");break;<br /> case 2:printf("x");break;<br /> case 3:printf("W");break;<br /> }<br /> }<br /> printf("/n");<br /> for(int i=1;i<=40;i++){<br />dish[i]=DNA[t_dish[i-1]+t_dish[i]+t_dish[i+1]];<br /> }<br /> }<br /> if(nCase!=0)printf("/n");<br /> }<br /> return 0;<br /> } 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.