2010 校慶盃乙組參考解答

//11677 - Alarm Clock (by Snail)

#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;

     }

 

 

//10921 - Find the Telephone (by Snail)

#include <iostream>

#include <string>

using namespace std;

int main () {

     int i;

     string s, x1="-01ABCDEFGHIJKLMNOPQRSTUVWXYZ", x2="-0122233344455566677778889999";

     while (cin >> s) {

           for (i=0; i<s.size(); i++)

                s[i] = x2[x1.find(s[i])];

           cout << s << endl;

     }

 

 

//11716 - Digital Fortress (By Snail)

#include <iostream>

#include <string>

#include <cmath>

using namespace std;

int main () {

     string s;

     int t, i, j, r;

     cin >> t;

     getline (cin, s);                             //讀掉t後面的換行

     while (t--) {

           getline (cin, s);

           r = (int) sqrt ((double)s.size());             //r(oot)--平方根

           if (r*r == s.size()) {                         //完全平方數

                for (i=0; i<r; i++)

                     for (j=i; j<(int)s.size(); j+=r)

                           cout << s[j];

                cout << endl;

           } else

                cout << "INVALID\n";

     }

}

 

 

//10922 - 2 the 9s (by Snail)

#include <iostream>

#include <string>

using namespace std;

int main () {

     string n;

     int i, s, t, d;

     while (cin >> n, n != "0") {

          s = 0;

          for (i=0; i<(int)n.size(); i++)          //求所有位數的數字和

                s += n[i] - '0';

           if (s % 9)                                     //不是9 的倍數

                cout << n << " is not a multiple of 9.\n";

           else {

                for (d=1; s>9; d++) {                     //d(egree)

                     for (t=s, s=0; t; t/=10)             //求t 的數字和

                           s += t % 10;

                }

                cout << n << " is a multiple of 9 and has 9-degree " << d << ".\n";

           }

     }

 

 

//11608 - No Problem (by Snail)--無陣列暴力版

#include <iostream>

using namespace std;

int main () {

     int s, r, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, x=1;

     while (cin >> s, s >= 0) {

           cin >> n1 >> n2 >> n3 >> n4 >> n5 >> n6 >> n7 >> n8 >> n9 >> n10 >> n11 >> n12;

           cout << "Case " << x++ << ":\n";

           cin >> r;                                       //一月

           if (s >= r) {                                   //題目足夠

                cout << "No problem! :D\n";

                s -= r;                                    //減掉用掉的題目

           } else

                cout << "No problem. :(\n";

           s += n1;                                        //加上本月所出題目

           cin >> r;                                       //二月

           if (s >= r) {

                cout << "No problem! :D\n";

                s -= r;

           } else

                cout << "No problem. :(\n";

           s += n2;

           //重覆恕刪

           cin >> r;                                       //十二月

           if (s >= r) {

                cout << "No problem! :D\n";

                s -= r;

           } else

                cout << "No problem. :(\n";

           s += n12;

     }

 

 

//10192 - Vacation (by Snail)

#include <iostream>

#include <string>

using namespace std;

int main() {

     int C[101][101], tc=1, i, j;

     string X, Y;

     while (getline(cin, X), X != "#") {

           getline(cin, Y);

           for (i=0; i<=X.size(); i++) C[i][0] = 0;

           for (j=1; j<=Y.size(); j++) C[0][j] = 0;

           for (i=1; i<=X.size(); i++)

                for (j=1; j<=Y.size(); j++)

                     if (X[i-1]==Y[j-1]) 

                           C[i][j] = C[i-1][j-1] + 1;

                     else

                           C[i][j] = max(C[i][j-1], C[i-1][j]);

           cout << "Case #" << tc++ << ": you can visit at most " << C[X.size()][Y.size()] << " cities.\n";

     }

}