檢查一個數字是否能被 41 整除, C++
我們在此將會看到一個程式,它可以檢查一個數字是否能被 41 整除。假設給定了一個數字 104413920565933。它是可以被 41 整除的。
為了檢查可整除性,我們需要遵循以下規則 −
每次提取該數字的末位數或截斷後的數字
從截斷後的數字中減去 4 * (之前計算出的數字的末位數)
重複以上步驟,直到有必要為止。
30873, so 3087 - 4*3 = 3075 3075, so 307 - 4 * 5 = 287 287, so 28 – 4 * 7 = 0 So, 30873 is divisible by 41.
示例
#include <iostream>
#include <algorithm>
using namespace std;
bool isDivisibleBy41(long long int n) {
while (n / 100) {
int last = n % 10;
n /= 10; // Truncating the number
n -= last * 4;
}
return (n % 41 == 0);
}
int main() {
long long number = 104413920565933;
if(isDivisibleBy41(number))
cout << "Divisible";
else
cout << "Not Divisible";
}輸出
Divisible
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP