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