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;
}