2016級電腦C++助教工作(11) 第三次上機解題報告__C++

來源:互聯網
上載者:User
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;}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.