參考程式碼如下:

int caluScore(string &answer, int rightScore, int unansweredScore, int wrongScore) {
    string reply;
    getline(cin,reply);

    int score = 0;
    int length = reply.size();
    for (int i=0;i<length;i++) {
        if (reply[i] == '.')
            break;
        if (reply[i] == ' ')
            score += unansweredScore;
        else if (reply[i] == answer[i])
            score += rightScore;
        // means wrong
        else
            score += wrongScore;
    }

    if (score>100)
        score = 100;
    if (score<0)
        score = 0;

    return score;
}

ps 不太懂為什麼題目要傳參 &answer

return ;

}

* 所有的燈號做一次的切換 ( `setAll` )
```c++
void setAll(int sizeOfLight) {
    for (int i=1;i<=sizeOfLight;i++) {
        switch (light[i]) {
            case 'G':
                light[i] = 'Y';
                break;
            case 'Y':
                light[i] = 'R';
                break;
            case 'R':
                light[i] = 'G';
                break;
            // means Error
            default:
                light[i] = 'E';
                break;
        }
    }

    return ;
}
  • 改變指定電燈 ( changeLight
string changeLight(int index, char color) {
    if (color == light[index])
        return "wrong";

    light[index] = color;
    return "ok";
}
  • 查詢電燈 ( searchLight
char searchLight(int index) {
    return light[index];
}
  • 更改指定顏色的電燈 ( DIYLight
void DIYLight(char firstColor, char secondColor, int sizeOfLight) {
    for (int i=1;i<=sizeOfLight;i++) {
        if (light[i] == firstColor)
            light[i] = secondColor;
    }

    return ;
}
  • 主程式 ( main
int main()
{
    int sizeOfLight;
    cin>>sizeOfLight;
    setUp(sizeOfLight);

    int index;
    char command, color;
    while (cin>>command) {
        if (command == 'A') {
            setAll(sizeOfLight);
        }
        else if (command == 'C') {
            cin>>index>>color;
            string status = changeLight(index,color);
            if (status == "wrong")
                cout<<status<<endl;
        }
        else if (command == 'S') {
            cin>>index;
            color = searchLight(index);
            cout<<color<<endl;
        }
        else if (command == 'X') {
            char firstColor, secondColor;
            cin>>firstColor>>secondColor;
            DIYLight(firstColor,secondColor,sizeOfLight);
        }
        // means Error
        else {
            cout<<"Error"<<endl;
        }
    }
}
  • Q:有些地方題目沒有要求要寫出來 ( 例如 Error ), 但為什麼會那樣寫?
  • A:因為方便 debug

參考程式碼如下:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int times;
    while (cin>>times) {
        int treasure[50] = {0};

        if (times == 0)
            break;

        for (int i=0;i<times;i++)
            cin>>treasure[i];

        int start = treasure[0];
        int distance = 0;
        for (int i=1;i<times;i++) {
            int distanceMin = 1001;
            int index;
            for (int j=1;j<times;j++) {
                if (treasure[j] == 1001)
                    continue;

                if (abs(start - treasure[j]) < distanceMin) {
                    distanceMin = abs(start - treasure[j]);
                    index = j;
                }
            }
            distance += distanceMin;
            start = treasure[index];
            treasure[index] = 1001;
        }
        cout<<distance<<endl;
    }
}

第四題:密室大逃脫

參考程式碼如下:

#include <iostream>
using namespace std;

void binary(long long int number);

int main()
{
    long long int a,b;
    while (cin>>a>>b) {
        if (a == 0 && b == 0)
            break;
        binary(a^b);
    }
}

void binary(long long int number) {
    string result;
    while (number > 0) {
        result += char(number % 2 + '0');
        number /= 2;
    }

    int length = result.size();
    for (int i=49;i>=0;i--) {
        if (i>=length)
            cout<<"0";
        else
            cout<<result[i];
    }
    cout<<endl;
}

參考程式碼如下:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    long long int year;
    while (cin>>year) {
        if (year == -1)
            break;

        if (year < 10)
            cout<<year<<endl;
        else {
            long long int sum = 0;
            int exponent = 0;
            while (year > 10) {
                sum += 10 * pow(2,exponent);
                year -= 10;
                exponent++;
            }
            sum += year * pow(2,exponent);
            cout<<sum<<endl;
        }
    }
}

有任何問題,歡迎私訊喔!