ZJANS

d124. 3的倍数

Easy Last Update: 2026/01/25
數學大數

給一數字 n
判斷 n 是不是 3 的倍數

n 很大:1010001n1010001-10^{10001} \leq n \leq 10^{10001}


解法一

判斷 3 的倍數的方法:
將每位數相加再 mod 3

✅ 完整代碼

評分結果(參考) : AC (1ms, 336KB)

#include<bits/stdc++.h>
#define int long long
using namespace std;

bool mod3eq0(string& s){
    int sum = 0;
    for(char c : s){
        if(c == '-') continue;
        sum += c-'0';
    }
    return sum%3 == 0;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string s;
    while(cin >> s){
        if(mod3eq0(s)) cout << "yes\n";
        else cout << "no\n";
    }
    
    return 0;
}