上機考程式碼答案

每頁標題為超連結 可跳轉至zerojudge

-葉子齊-

基礎班

#include <iostream>
using namespace std;

int dino(int x)
{
    if (x==1) return 1; 
    //第一級為最小單位 僅需一張紙回傳1
    else return 1+3*dino(x-1); 
    //1為恐龍本身+3隻小自己一級的恐龍
}

int main()
{
    int n;
    cin>>n; //要摺出第n級恐龍
    cout<<dino(n);

    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string text[50];
    string key;
    int count = 0;
    cin >> key;
    
    for(int i=0; ; i++){
        cin >> text[i];
        if(text[i] == key) count++;
        else if (text[i] == "#") break;
    }
    
    cout << count << endl;
    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n, number[100];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> number[i];
    }
    for (int i = n - 1; i >= 0; i--) {
        cout << number[i] << " ";
    }
    cout << endl;
    return 0;
}
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int N;
    cin >> N;
    int memes[100];
    for (int i = 0; i < N; i++) {
        cin >> memes[i];
    }

    bubbleSort(memes, N);

    for (int i = 0; i < N; i++) {
        cout << memes[i];
        if (i != N - 1) cout << " ";
    }
    cout << "\n";

    return 0;
}

進階班

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

struct Book
{
    int weight; //書本重量
    int value; //書本效益
    char level; //書本級別
}book[15]; //陣列儲存書本數值,書本最多10本 

int main()
{
    int m,n; //書本數量和背包容量
    cin>>m>>n;
    for(int i=0;i<m;i++)
        cin>>book[i].weight>>book[i].level;
    for(int i=0;i<m;i++)
    {
        if(book[i].level=='A')
            book[i].value=book[i].weight*book[i].weight;
        else if(book[i].level=='B')
            book[i].value=3*book[i].weight+2;
        else if(book[i].level=='C')
            book[i].value=(book[i].weight*(book[i].weight+1))/2;
        else book[i].value=book[i].weight;
    } //判斷書的級別進而知道書本效益
    int dp[m+1][n+1]; 
    //建dp的表格 +1是因為怕溢出
    memset (dp,0,sizeof(dp)); //初始化將表格清空
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<=n;j++)
        {
            if(book[i].weight<=j)
                dp[i+1][j]=max(dp[i][j],dp[i][j-book[i].weight]+book[i].value);
            else dp[i+1][j]=dp[i][j];
        }
    }
    cout<<dp[m][n]<<endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

struct Student {
    string name;
    int id;
    int scores[3];
    float sum;
    int rank;
};

bool compare(Student &a, Student &b){
        return a.sum > b.sum;
}

int main() {
    int n;
    cin >> n;
    Student stu[10];

    // 輸入
    for (int i = 0; i < n; i++) {
        cin >> stu[i].name >> stu[i].id
            >> stu[i].scores[0] >> stu[i].scores[1] >> stu[i].scores[2];
        stu[i].sum = stu[i].scores[0]*0.5 + stu[i].scores[1]*0.3 + stu[i].scores[2]*0.2;
    }
    
    sort(stu, stu + n, compare);
    
    for (int i = 0; i < n; i++) {
        cout << stu[i].name << ' ' << stu[i].id << ' ' << stu[i].sum << endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int arr[], int n) {
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n - i; ++j) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
}

int bs(int arr[], int n, int target, int &comparisons) {
    int left = 1, right = n;
    comparisons = 0;

    while (left <= right) {
        comparisons++;
        int mid = (left + right) / 2;

        if (arr[mid] == target) {
            return comparisons;
        } else if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return -1;
}

int main() {
    int n;
    cin >> n;

    int arr[n + 1];
    for (int i = 1; i <= n; ++i) {
        cin >> arr[i];
    }

    int target;
    cin >> target;

    bubbleSort(arr, n);

    int comparisons;
    int result = bs(arr, n, target, comparisons);

    if (result == -1) {
        cout << "not found!" << endl;
    } else {
        cout << comparisons << endl;
    }

    return 0;
}
#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n;
    cin >> n;
    queue<string> sister, classmate, stranger;

    for (int i = 0; i < n; i++) {
        string name, level;
        cin >> name >> level;
        if (level == "SISTER") sister.push(name);
        else if (level == "CLASSMATE") classmate.push(name);
        else stranger.push(name);
    }

    while (!sister.empty()) {
        cout << sister.front() << endl;
        sister.pop();
    }
    while (!classmate.empty()) {
        cout << classmate.front() << endl;
        classmate.pop();
    }
    while (!stranger.empty()) {
        cout << stranger.front() << endl;
        stranger.pop();
    }

    return 0;
}

上機考程式碼答案 - 下學期

By strange_swing

上機考程式碼答案 - 下學期

  • 132