UVA一些简单题题解。
UVA 272 TEX Quotes(字符串處理)
題目大意是: 在Tex中,左雙引號是“ `` ”.右雙引號是 “ ‘’ ” ,輸入一篇包含雙引號的文章,任務是把他轉換成 Tex的格式
#include <iostream> #include <cstdio> using namespace std; int main() {char c;while((c=getchar())!=EOF){bool tag; // tag=false 代表是左引號,true代表右引號if(!tag && c=='"'){cout<<"``";tag=true;}else if(tag && c=='"'){cout<<"''";tag=false;}else cout<<c;}return 0; }UVA10474 Where is the Marble?(二分題)
AC代碼:
UVA10340 All int all(字符串處理)
AC代碼:
UVA-10935
Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom.
The following operation is performed as long as there are at least two cards in the deck:
Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck.
Your task is to find the sequence of discarded cards and the last, remaining card.
Input
Each line of input (except the last) contains a
number n ≤ 50. The last line contains ‘0’ and
this line should not be processed.
Output
For each number from the input produce two
lines of output. The first line presents the sequence
of discarded cards, the second line reports
the last remaining card. No line will have
leading or trailing spaces. See the sample for the
expected format.
題目意思是:給定一個 1~n的卡片,從上往下排(也就是說最上面一張是1,然后是2,是3,以此類推)
然后操作的方法是:拿走最頂端的一張,然后將新的頂端的那張卡片放到底端,直到最后只剩下一張卡片
問我們移出的卡片是什么,以及最后剩下來的卡片是什么。
我們可以直接模擬這個操作過程來做。
AC代碼如下:
uva-227 Puzzles
題目的意思其實很簡單…也就是說先給你一個5*5的字符矩陣,然后其中有一個空格子(有一點像那個八數碼)
然后給定你操作,你只要模擬操作即可,ABLF分別代表了不同的操作。
AC代碼:
#include <iostream> #include <cstdio> using namespace std; int main() {string a[5]; //總共5行int kase=1; int countt=0;while(1){for(int i=0;i<5;i++) //輸入五行{getline(cin,a[i]); if(a[i].length()<5) //這里是因為他輸入數據中 如果空白在最后一格的話沒有敲入空格所以我們需要人為添加 a[i][4]=' ';if(a[0]=="Z") //輸入數據 當Z的時候就退出break;}int col,row;for(int i=0;i<5;i++)for(int j=0;j<5;j++)if(a[i][j]==' '){col=i; // col 代表行row=j; // row 代表列}if(a[0]=="Z")break;if(countt) //格式控制(每兩個puzzle間要用空行隔開)cout<<endl;countt++;cout<<"Puzzle #"<<kase<<':'<<endl;kase++;string introduction;char c;while((c=getchar())!='0') //他的操作是當為0的時候停止introduction+=c;int tag=1;for(int i=0;i<introduction.length();i++){ //進行每一個操作if(introduction[i]=='A'){ //上移if(col-1>=0){a[col][row]=a[col-1][row];a[col-1][row]=' ';col=col-1;}else{cout<<"This puzzle has no final configuration."<<endl;tag=0;break;}}if(introduction[i]=='B') //下移{if(col+1<=4){a[col][row]=a[col+1][row];a[col+1][row]=' ';col=col+1;}else{cout<<"This puzzle has no final configuration."<<endl;tag=0;break;}}if(introduction[i]=='L') //左移{if(row-1>=0){a[col][row]=a[col][row-1];a[col][row-1]=' ';row=row-1;}else{cout<<"This puzzle has no final configuration."<<endl;tag=0;break;}}if(introduction[i]=='R') //右移{if(row+1<=4){a[col][row]=a[col][row+1];a[col][row+1]=' ';row=row+1;}else{cout<<"This puzzle has no final configuration."<<endl;tag=0;break;}}}if(tag){ //代表著操作過程中沒有超出范圍(那么就輸出解)for(int i=0;i<5;i++) {for(int j=0;j<5;j++)if(j==0)cout<<a[i][j];else cout<<' '<<a[i][j];cout<<endl;}}getchar();}return 0; }總結
以上是生活随笔為你收集整理的UVA一些简单题题解。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手把手教你通过solidworks模拟摩
- 下一篇: 数据可视化之matplotlib实战:p