ตรวจสอบเลขใหญ่ (string) หาร 75 ลงตัวหรือไม่
กฎ: หาร 75 ลงตัว ⇔ หาร 3 ลงตัว และ หาร 25 ลงตัว
- หาร 3: ผลรวมหลักเลขหาร 3 ลงตัว
- หาร 25: 2 หลักสุดท้ายหาร 25 ลงตัว
ตัวอย่างโค้ด
#include <bits/stdc++.h>
using namespace std;
bool isDiv75(string num) {
int n = num.length();
long sum = accumulate(begin(num), end(num), 0) - '0' * n;
if (sum % 3 != 0) return false;
int last_two = (num[n-2]-'0')*10 + (num[n-1]-'0');
return (last_two % 25 == 0);
}
int main() {
string num = "1754586672150";
if (isDiv75(num)) cout << "Divisible";
else cout << "Not Divisible";
}
ผลลัพธ์
Divisible