日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVA一些简单题题解。

發布時間:2024/3/12 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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代碼:

#include <iostream> #include <algorithm> #include <cstdio> using namespace std; int a[10005]; int main() {int N,Q,kase=0;int ans;while(scanf("%d %d",&N,&Q)==2 && N){for(int i=0;i<N;i++)scanf("%d",&a[i]);sort(a,a+N);printf("CASE# %d:\n",++kase);for(int j=0;j<Q;j++){int number;scanf("%d",&number);int L=0,R=N,mid;while(L<=R){mid=L+(R-L)/2;if(number<a[mid]){R=mid-1;}else if(number>a[mid]){L=mid+1;}else if(number==a[mid]){ans=mid;R=mid-1;}}if(number==a[ans])printf("%d found at %d\n",number,ans+1);elseprintf("%d not found\n",number);}}return 0; }

UVA10340 All int all(字符串處理)
AC代碼:

#include <iostream> #include <cstring> using namespace std; int main() {string a,b;while(cin>>a>>b){int countt=0;int position=0;for(int i=0;i<a.length();i++){for(int j=position;j<b.length();j++){if(a[i]==b[j]){countt++;position=j+1;break;}}}if(countt==a.length())cout<<"Yes"<<endl;elsecout<<"No"<<endl;}return 0; }

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代碼如下:

#include <iostream> #include <vector> using namespace std; int main() {int n;while(cin>>n && n){vector<int> vec;for(int i=1;i<=n;i++)// 先把n個數都放入vec.push_back(i);int m=vec.size();if(n==1) // 因為可能僅僅就一張卡片那么就不用操作了..cout<<"Discarded cards:"<<endl;else cout<<"Discarded cards: ";while(m>1) //如果說還有至少2張卡片{if(m>2)cout<<*vec.begin()<<", ";else //這時候m<=2 最后一次操作cout<<*vec.begin()<<endl;vec.erase(vec.begin()); //移出最頂端的int j=*vec.begin();//使最頂端的卡片放到最后面vec.erase(vec.begin());vec.push_back(j);m--;}cout<<"Remaining card: "<<*vec.begin()<<endl;}return 0; }

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一些简单题题解。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。