回目錄

100-1 資訊社期末考參考解答

原 Google Sites 連結:/solutions/100-1-zi-xun-she-qi-mo-kao-can-kao-jie-da

第一題 d061: 曾祖母的慶生會


#include <iostream>
using namespace std;
int main () {
    int y;
    cin >> y;
    cout << 97 - y - (y < 0) << endl;
}

第二題 b202: A. 優惠方案


#include <iostream>
using namespace std;
int main () {
    int k, p1, p2, p3;
    cin >> k;
    while (k--) {
        cin >> p1 >> p2 >> p3;
        if (p1 != p2 && p2 != p3 && p1 != p3)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}

第三題 d669: 11677 - Alarm Clock


#include <iostream>
using namespace std;
int main () {
    int h1, m1, h2, m2;
    while (cin >> h1 >> m1 >> h2 >> m2, h1 + m1 + h2 + m2) {
        cout << ((h2*60 + m2) - (h1*60 + m1) + 1440) % 1440 << endl;
    }
}

第四題 d980: 11479 - Is this the easiest problem?


#include <iostream>
using namespace std;
int main () {
    long long T, x, a, b, c;
    cin >> T;
    for (x=1; x<=T; x++) {
        cin >> a >> b >> c;
        if (a <= 0 || b <= 0 || c <= 0 || a+b <= c || a+c <= b || b+c <= a)
            cout << "Case " << x << ": Invalid\n";
        else if (a == b && b == c)
            cout << "Case " << x << ": Equilateral\n";
        else if (a == b || b == c || c == a)
            cout << "Case " << x << ": Isosceles\n";
        else
            cout << "Case " << x << ": Scalene\n";
    }
}

第五題 b226: E. 鋪地磚

#include <iostream>
using namespace std;
int main () {
    int L, W, x, y;
    while (cin >> L >> W >> x >> y, L) {
        cout << ((L%x || W%y) && (L%y || W%x) ? -1 : L * W / x / y) << endl;
    }            // 若直擺不行    橫擺也不行
}

第六題 b080: A. 畢氏定理

#include <iostream>
#include <cmath>
using namespace std;

int main () {
    int a, b, c, c2;                            // c2--c 平方
    while (cin >> a >> b, a) {
        c2 = a*a + b*b;                         // 假設 c 為斜邊
        c = (int)sqrt((double)c2);
        if (c2 == c*c)
            cout << c << endl;
        else {
            if (a < b) swap (a, b);             // 確定 a > b
            c2 = a*a - b*b;                     // 假設 a 為斜邊
            c = (int)sqrt((double)c2);
            if (c2 == c*c)
                cout << c << endl;
            else
                cout << "Wrong\n";
        }
    }
}

第七題 a132: 10931 - Parity

#include <iostream>
#include <string>
using namespace std;

int main () {
    int n, i, s;
    string b, d="01";
    while (cin >> n, n) {
        s = 0;
        b = "";
        while (n) {
            s += n % 2;
            b = d[n % 2] + b;
            n /= 2;
        }
        cout << "The parity of " << b << " is " << s << " (mod 2).\n";
    }
}

第八題 b225: D. 棒球練習

#include <iostream>
using namespace std;
int main () {
    int N;
    double x1, y1, x2, y2, x3, y3;
    cin >> N;
    while (N--) {
        cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
        cout << ((x2-x1)*(y3-y1) == (x3-x1)*(y2-y1) ? "NO\n" : "YES\n");
    }                // 若斜率相等則不成一個三角形
}

第九題 b072: D. 打不倒的空氣人

#include <iostream>
#include <string>
using namespace std;

int main () {
    string s, w, pw;
    int n, m, A;
    while (cin >> n >> m, n) {
        s = " ";                                // 用一個空白佔住 s[0]
        while (n--) {
            cin >> w;                           // w(ord)-- 英文單字
            s += w;                             // s = s + w; 會超時
        }
        pw = "";                                // p(ass)w(ord)-- 密碼
        while (m--) {
            cin >> A;
            pw += s[A];
        }
        cout << pw << endl;
    }
}

第十題 a130: 12015 - Google is Feeling Lucky

#include <iostream>
#include <string>
using namespace std;

int main () {
    int i, mxv, v[10], t, tc=1;
    string u[10];                           // u(rl)-- 網址
    cin >> t;
    while (t--) {
        mxv = 0;                            // max v-- 最大相關度
        for (i=0; i<10; i++) {
            cin >> u[i] >> v[i];
            mxv = max (mxv, v[i]);          // 求 v[i] 最大值
        }
        cout << "Case #" << tc++ << ":\n";
        for (i=0; i<10; i++)
            if (v[i] == mxv)                // 若相關度最高
                cout << u[i] << endl;
    }
}