【CPE】一顆星選集(Part 12) | C++

更新於 發佈於 閱讀時間約 7 分鐘

2 the 9s

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1863

中文題目:https://zerojudge.tw/ShowProblem?problemid=d672

#include <iostream>
using namespace std;
 
int func(string s, int count){
    int temp = 0, total = 0;
    for (int i = 0; i < s.length(); i++){
        temp = s[i] - '0';
        total += temp;
    }
    count++;
    if (total == 9){
        return count;
    }else if(total < 9){
        return 0;
    }else{
        return func(to_string(total), count);
    }
}

int main(){
    int ans;
    string s;
    while (cin >> s){
        if (s == "0") break;
        ans = func(s, 0);
        if (ans == 0){
            cout << s << " is not a multiple of 9.\n";
        }else{
            cout << s << " is a multiple of 9 and has 9-degree " << ans << ".\n";
        }
    }
return 0;
}

GCD

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2412

中文題目:https://zerojudge.tw/ShowProblem?problemid=d255

#include <iostream>
#include <algorithm>
using namespace std;
  
int GCD(int x, int y) {
    while ((x %= y) && (y %= x));
    return x + y;
}
  
int main() {
    int N, G;
    while (cin >> N && N) {
        G = 0;
        for (int i = 1; i < N; i++) {
            for (int j = i + 1; j <= N; j++) {
                G += GCD(i, j);
            }
        }
        cout << G << "\n";
    }
return 0;
}

Largest Square

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1849#google_vignette

中文題目:https://zerojudge.tw/ShowProblem?problemid=e575

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
 
int t, m, n, q, a[105][105], mx[105][105], x, y, ans;
char c;
 
int main() {
    memset(a, -1, sizeof(a));
    cin >> t;
    while (t--){
        cin >> m >> n >> q;
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                cin >> c;
                a[i][j] = (int)c;
                if (i == 0 || j == 0) mx[i][j] = 1;
                else if (a[i][j] == a[i-1][j] && a[i][j] == a[i-1][j-1] && a[i][j] == a[i][j-1]){
                    mx[i][j] = min({mx[i-1][j], mx[i-1][j-1], mx[i][j-1]})+1 ;
                }
                else mx[i][j] = 1;
            }
        }
        cout << m << " " << n << " " << q << "\n";
        while (q--){
            cin >> x >> y;
            ans = 1;
            for (int i = 1; i <= 100; i++){
                if (x+i > m || y+i > n) break;
                if (mx[x+i][y+i] >= ans+2) ans += 2;
                else break;
            }
            cout << ans << "\n";
        }
    }
return 0;
}



留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
0會員
30內容數
Jim的沙龍的其他內容
2025/08/14
All You Need Is Love! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1134 中文題目:https://z
2025/08/14
All You Need Is Love! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1134 中文題目:https://z
2025/08/14
Parity 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1872 中文題目:https://zerojudge.tw/Sho
2025/08/14
Parity 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1872 中文題目:https://zerojudge.tw/Sho
2025/08/14
An Easy Problem! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1034 #include <iostream>
2025/08/14
An Easy Problem! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1034 #include <iostream>
看更多