[解题报告]《算法竞赛入门经典》基础题Volume 0. Getting Started Uva10055 Uva10071 Uva10300 Uva458 Uva494 Uva490 Uva445 Uva488 Uva489 Uva694 Uva457

我爱A水题...嘿嘿...

 

UVA10055 - Hashmat the Brave Warrior

入门题,注意数据类型为longlong即可


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

 


UVA10071 - Back to High School Physics

入门题

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

10300 - Ecological Premium

题目没仔细看,就是把每一行第一个和第三个数的积求出来即可

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

 

 

UVA458 - The Decoder

入门题,每个字符等差变换

 

 

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

 

 

UVA494 - Kindergarten Counting Game

 

数单词,找出有多少个连续字母串即可

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

 

 

UVA490 - Rotating Sentences

 

有点类似求矩阵的倒置,先找出最长的串作为矩阵高,注意不够串长的地方输出空格

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

 

 

UVA445 - Marvelous Mazes

每个字符输出的个数与它前面的数字有关.数字都看做一位数,求和即可,注意b是空格.遇到!换行.

 

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

 

 

UVA488 - Triangle Wave

用循环结构打印字符串的题目..注意对空行的处理..煞是恶心..

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

 

 

UVA489 - Hangman Judge

貌似以前还玩过这个游戏,错了七次就输了,再错七次前猜对就赢了,还没错七次结束就是胆怯了

设一个标志数组,对应正确答案的每个字符,为1时说明该位置猜对了

对所猜字符串依次判断,猜对则设标志数组相应位置为1,并累积正确字符个数,如果不正确,错误次数加一

如果错七次或者全部猜对退出游戏,如果退出时没有输赢,则是胆怯了

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

 

 

 

UVA694 - The Collatz Sequence

3n+1问题..偶数除2,奇数3n+1,直到为1或者超出limit为止

 

 

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

 

 

 

 

UVA457 - Linear Cellular Automata

看了好久才看懂题目..E文不好的悲剧啊..

就是有40个培养皿,里面有细菌,培养皿共有4个状态,分别为0,1,2,3

初始第20个为1,其余为0

第二天,培养皿的状态取决于它本身以及周边两个培养皿的和,假设和为s

题目要求输入一个数组DNA[10],值为0~3

那么第二天该培养皿的状态就为DNA[s]

一直循环,直到50为止,中间用以个临时数组保存前一天的状态

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

 

 

 

 

你可能感兴趣的:(游戏,c,算法,String,iostream)