A ants
只需要計算螞蟻離兩端遠近關係,
最短時間是所有螞蟻都快速到達端點的時間
最長時間是其中一隻螞蟻到端點的最長時間
#include<iostream>#include<stack>#include<algorithm>using namespace std;int main(){ int n; cin>>n; while(n--){ int len,m,minx=0,maxx=0,u; cin>>len>>m; for(int i = 0;i < m; i++){ cin>>u; minx = max(minx,min(u,len-u)); maxx = max(maxx,max(u,len-u)); } cout<<minx<<" "<<maxx<<endl; }}
B. Number Sequence 類比即可
#include<iostream>using namespace std;int main(){ int N; while(cin>>N) { if(N==0) break; int number=1; while(1) { if(N==1) break; if(N%2==0) N=N/2; else N=3*N+1; number++; } cout<<number<<endl; } return 0;}
C. Rails 這道題用一個資料結構,棧來維護在站中的火車標號,由於出站的順序是固定的,我們只要關心出站的火車編號 如果現在需要一個編號為x的火車出站,棧頂的標號是x則把x出棧,否則不斷把還未進棧的火車進棧,知道出現x進入棧頂即可
#include <iostream>#include <string>#include <cstdio>#include <map>using namespace std;int main(){ int out[1002],stack[1002],n,t; while(cin>>n,n){ while(cin>>out[0],out[0]){ for(int i = 1;i < n; i++) cin>>out[i]; int in = 1, stack_point = 0; int out_point = 0; for(;out_point < n; out_point++){ while((in <= n) && (stack_point == 0 || stack[stack_point-1] != out[out_point])){ stack[stack_point++] = in++; } if(stack[stack_point-1] != out[out_point]) break; stack_point--; } if(out_point == n) cout << "Yes" << endl; else cout << "No" << endl; } cout<<endl; }}
D. The Worm Turns 這道題記錄20個位置,每走一步,蛇尾消失,蛇頭前進一步 然後判斷蛇頭會不會跟身體觸碰,以及是否出界即可
#include <iostream>using namespace std;int main(){ int n; char step[104]; int a[23]; int b[23]; while(cin>>n,n!=0){ cin>>step; for(int i=1,j=30;i<=20;i++,j--){ b[i] = j; a[i]=25; } int i; for(i=0;i<n;i++){ for(int k=20;k>1;k--){ b[k]=b[k-1]; a[k]=a[k-1]; } if(step[i]=='N') a[1]=a[1]-1; else if(step[i]=='S') a[1]=a[1]+1; else if(step[i]=='W') b[1]=b[1]-1; else if(step[i]=='E') b[1]=b[1]+1; if(b[1]<1||b[1]>50||a[1]<1||a[1]>50){ cout<<"The worm ran off the board on move "<<i+1<<"."<<endl; break; } int j; for(j=2;j<=20;j++){ if(a[1]==a[j]&&b[1]==b[j]){ cout<<"The worm ran into itself on move "<<i+1<<"."<<endl; break; } } if(j!=21) break; } if(i==n) cout<<"The worm successfully made all "<<n<<" moves."<<endl; } return 0;}
E. Cantoring Along 遞迴實現或者迭代實現都可以,這裡舉一個遞迴實現的方法, 對於一個n,實際上可以分解為 三個子問題,n-1問題,3^n個空格,n-1問題
#include<iostream>using namespace std;void haha(int a){ if(a==1) cout<<"-"; else { haha(a/3); for(int i=0;i<a/3;i++) cout<<" "; haha(a/3); }}int main(){ int n; while(cin>>n) { int b=1; for(int i=0;i<n;i++) { b=b*3; } haha(b); cout<<endl; } return 0;}
F. Football Foundation (FOFO) 建立一張地表徵圖記,如果走過了記錄是第幾步走的(這裡第一步算0,所以輸出要+1), 如果走到一個已走的地方,那麼就是迴圈了,如果出界了就記錄一下。
#include <iostream>#include <cstring>using namespace std;int main(){ int row,colume,start,flag[100][100]; char map[100][100]; while(cin>>row>>colume>>start,row+colume+start){ for(int i = 0;i < row; i++) cin>>map[i]; memset(flag,-1,sizeof(flag)); int x = 0, y = start-1; int step = 0; while(true){ if(flag[x][y] != -1){ cout<<flag[x][y]<<" step(s) before a loop of "<<step-flag[x][y]<<" step(s)"<<endl; break; } flag[x][y] = step++; if(map[x][y] == 'N') x--; else if(map[x][y] == 'S') x++; else if(map[x][y] == 'E') y++; else if(map[x][y] == 'W') y--; if(x < 0 || y < 0 || x >= row || y >= colume){ cout<<step<<" step(s) to exit"<<endl; break; } } }}
G. Web Navigation
類比棧的操作,這裡用到一個vector的資料結構,這個可以自己用數組實現。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;int main(){ vector<string> web; web.push_back("http://www.acm.org/"); string type,word; int num = 1; while(cin>>type){ if(type == "QUIT") break; if(type == "VISIT"){ cin>>word; while(web.size() > num) web.pop_back(); web.push_back(word); num++; cout<<word<<endl; } if(type == "BACK"){ if(num <= 1) cout<<"Ignored"<<endl; else { num--; cout<<web[num-1]<<endl; } } if(type == "FORWARD"){ if(num == web.size()) cout<<"Ignored"<<endl; else { num++; cout<<web[num-1]<<endl; } } } return 0;}