2016-10-29 APCS 實作題


官方題本: https://apcs.csie.ntnu.edu.tw/wp-content/uploads/2018/12/1051029APCSImplementation.pdf

c294: 三角形辨別

題目連結:https://zerojudge.tw/ShowProblem?problemid=c294

#include <bits/stdc++.h>
using namespace std;
int main(){

    int a[3];
    cin >>a[0]>>a[1] >>a[2];
    sort(a,a+3);
    cout << a[0] << " " << a[1] << " " << a[2] << endl;

    if (a[0] + a[1] <= a[2]){
        cout << "No" << endl;
    }else if (a[0]*a[0]+a[1]*a[1] < a[2]*a[2]){
        cout << "Obtuse" << endl;
    }else if (a[0]*a[0]+a[1]*a[1] == a[2]*a[2]){
        cout << "Right" << endl;
    }else if (a[0]*a[0]+a[1]*a[1] > a[2]*a[2]){
        cout << "Acute" << endl;
    }

}

c295: 最大和

題目連結:https://zerojudge.tw/ShowProblem?problemid=c295

#include <iostream>
using namespace std;
int main(){
    int num[20][20];
    int max[20];
    int r,c,sum;
    bool first;

    while (cin >> r >> c){

        sum=0;
        //取得每一列的最大值
        for(int i=0;i<r;i++){
            max[i]=0;
            for(int j=0;j<c;j++){
                cin >> num[i][j];
                if (max[i]<num[i][j]) max[i]=num[i][j];
            }
        }

        ///算出最大總和
        for(int i=0;i<r;i++){
            sum=sum+max[i];
        }

        cout << sum << endl;

        first=true;
        for(int i=0;i<r;i++){
            if (sum%max[i]==0) {
                if (first){
                    cout << max[i];
                    first=false;
                }else{
                    cout << " " << max[i];
                }
            }
        }
        if (first) cout << -1;
        cout << endl;
    }
}

c296: 定時K彈

題目連結:https://zerojudge.tw/ShowProblem?problemid=c296

AC解答

#include<bits/stdc++.h>
using namespace std;

int main(){

    vector<int> members;   //vector containing numbers
    int n,m,k;
    int currentBang = 0;
    int bombIndex = 0;

    cin >> n >> m >> k;

    for(int i=0; i<n ; i++){
        members.push_back(i+1);
    }

    while( currentBang < k ){
        bombIndex += m-1;
        bombIndex = bombIndex%(members.size());
        members.erase(members.begin()+bombIndex);
        currentBang++;
    }

    bombIndex = bombIndex%(members.size());
    cout<<members[bombIndex]<<endl;

}

TLE解答

#include<bits/stdc++.h>
using namespace std;

int main(){
    bool bang[200000];
    int n,m,k;
    int currentBang=0;;

    cin>>n>>m>>k;

    for(int i =1;i<=n;i++){
        bang[i]=false;
    }


    int bombIndex = 1 ;
    while(currentBang<k){
        int cnt=0;
        while(cnt<m){
            if(bang[bombIndex]==false){
                cnt++;
            }
            if(cnt==m)break;

            bombIndex++;
            if(bombIndex>n){
                bombIndex=1;
            }
        }
        bang[bombIndex]=true;
        currentBang++;
    }

    while(bang[bombIndex]==true){
        bombIndex++;
        if(bombIndex>n){
                bombIndex=1;
            }
    }
    cout<<bombIndex;
}

c297: 棒球遊戲

題目連結:https://zerojudge.tw/ShowProblem?problemid=c297

#include <bits/stdc++.h>

using namespace std;
bool b[4];

int convert(string s) {
    if (s=="1B") return 1;
    if (s=="2B") return 2;
    if (s=="3B") return 3;
    if (s=="HR") return 4;
    if (s=="FO") return 0;
    if (s=="GO") return 0;
    if (s=="SO") return 0;
}

int win(int c) {//c為1表示一壘打, c為2表示二壘打。

    int s = 0;
    for (int i = 3; i>3 - c; i--) {//已經在壘包上的得分
        if (b[i]) s++;
    }

    for (int i = 3; i>c; i--) {//已經在壘包上往前推進
        b[i] = b[i - c];
    }

    if (c == 4) s++; //全壘打
    else b[c] = true;//打擊者上壘


    for (int i = 1; i<c; i++) b[i] = false;//往前推進,壘包設定為沒有人

    return s;//回傳得分
}

int main() {

    int p[9][20], n[9], out, ocount, score;
    string s;
    //輸入選手資料
    for (int i = 0; i<9; i++) {
        cin >> n[i];
        for (int j = 0; j<n[i]; j++) {
            cin >> s;
            p[i][j] = convert(s);
        }
    }

    cin >> out;
    ocount = 0;
    score = 0;

    //壘包上有沒有人
    for (int i = 1; i<4; i++) b[i] = false;

    for (int i = 0; i<n[i] && ocount<out; i++) {
        for (int j = 0; j< 9 && ocount<out; j++) {
            if (p[j][i] > 0) {//1B 2B 3B HR
                score += win(p[j][i]);
            } else {
                ocount++;//出局
                if (ocount % 3 == 0) {//三出局
                    //壘包清空
                    for (int k = 1; k<4; k++) b[k] = false;
                }
            }
        }
    }
    cout << score << endl;
}

results matching ""

    No results matching ""