回目錄

102-2 資訊第一次小考參考解答

原 Google Sites 連結:/solutions/102-2-zi-xun-di-yi-ci-xiao-kao-can-kao-jie-da

第一題

//a040: 阿姆斯壯數 by Snail

#include <iostream>

using namespace std;

int main () {
    int i, j, a, b, arm[21]={1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1000001};

/*
    int k, r, n, x, e, d[7], narm=0;  // 求陣列中的值
    for (i=1; i<=1000000; i++) {
      r = i;
      for (e=0; r; e++) {
          d[e] = r % 10;
          r /= 10;
      }
      n = 0;
      for (j=0; j<e; j++) {
          x = 1;
          for (k=0; k<e; k++)
              x *= d[j];
          n += x;
      }
      if (n == i)
          arm[narm++] = n;
    }
    arm[narm] = 1000001;              // 陣列尾端記號
*/

    while (cin >> a >> b) {
        for (i=0; arm[i]<a; i++);
        for (j=i; arm[j]<=b; j++)
            cout << arm[j] << ' ';
        cout << (i==j ? "none\n" : "\n");
    }
}

第二題

//11608 - No Problem (by Snail)
#include<iostream>
using namespace std;

int main () {
    int s, i, a[100], b, t=1;
    while (cin >> s, s>=0) {
        for (i=0; i<12; i++)
            cin >> a[i];
        cout << "Case " << t++ << ":" << endl;
        for (i=0; i<12; i++) {
            cin >> b;
            if(b>s)
                cout << "No problem. :(" << endl;
            else {
                cout << "No problem! :D" << endl;
                s -= b;
            }
            s += a[i];
        }
    }
}

第三題

//d212. 東東爬階梯 by Snail
#include <iostream>
using namespace std;

int main () {
    unsigned long long s[93] = {1, 1};
    int i, n;
    for (i=2; i<=92; i++)
        s[i] = s[i-1] + s[i-2];
    i = 0;
    while (cin >> n)
        i=max(i, n), cout << s[n] << endl;
}

第四題

//382 - Perfection (by Snail)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main () {
    int s, n, f;
    string output[3] = {"DEFICIENT", "PERFECT", "ABUNDANT"};
    cout << "PERFECTION OUTPUT\n";
    while (cin >> n, n) {
        s = n>1;
        for (f=2; f*f<n; f++)
            if (n % f == 0)
                s += f + n/f;
        if (f*f == n)
            s += f;
        cout << setw(5) << n << "  " << output[(s>=n)+(s>n)] << endl;
    }
    cout << "END OF OUTPUT\n";
}

第五題

//10924 - Prime Words (by Snail)
#include <iostream>
#include <string>
using namespace std;

int main () {
    int i, n, n2;
    string s;
    while (cin >> s) {
        n = 0;
        for (i=0; i<(int)s.size(); i++)
            n += (s[i] < 'a' ? s[i] - 38 : s[i] - 96);
        for (i=2; n%i && i*i<=n; i++);                  // 求第一個 <= 平方根的因數
        cout << (i*i > n ? "It is a prime word.\n" : "It is not a prime word.\n");
    }
}

第六題

//D. 跑跑卡丁車 -- 2009 NPSC 高中組決賽 by Snail
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct racer {
    string n, t;
    int sn;
} r[100000];

bool timelt (racer a, racer b) {return a.t < b.t;}
bool snlt (racer a, racer b) {return a.sn < b.sn;}

int main () {
    int n, i, k, h, m;
    char ch;
    while (cin >> n, n) {
        for (i=0; i<n; i++) {
            cin >> r[i].n >> r[i].t;
            r[i].sn = i;                        // s(erial) n(umber) -- 序號
        }
        sort (r, r+n, timelt);                  // 依時間由小到大排序
        for (k=n/3; k<n && r[k].t==r[k-1].t; k++);// 同分增額錄取
        sort (r, r+k, snlt);                    // 依序號由小到大排序
        cout << "LIST START\n";
        for (i=0; i<k; i++)
            cout << r[i].name << endl;
        cout << "LIST END\n";
    }
}
//D. 跑跑卡丁車 -- 2009 NPSC 高中組決賽 by kevin704300 (邱玉全)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string a[100000], b[100000], c[100000];

int main () {
    int n, i, j, m;
    while (cin >> n, n) {
        cout << "LIST START\n";
        for (i=0; i<n; i++)
            cin >> a[i] >> b[i], c[i] = b[i];
        sort(c, c+n);
        m = n/3 - 1;
        for (i=0; i<n; i++)
            if (b[i] <= c[m])
                cout << a[i] << endl;
        cout << "LIST END\n";
    }
}