判斷倍數的重點就是不要想太多
對了!!還要記得字串跟陣列一樣
索引值是從 0 ~ s.size ()-1 喔~~
# 2 的倍數
個位數字為偶數 (含 0)
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
if((s[length-1]-'0')%2==0) | |
cout<<s<<"是2的倍數\n"; | |
else | |
cout<<s<<"不是2的倍數\n"; | |
} | |
return 0; | |
} |
# 3 的倍數
各個數字和為 3 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int tmp = 0; | |
for(int i = 0;i<length;i++){ | |
tmp += s[i] - '0'; | |
} | |
if(tmp%3==0) | |
cout<<s<<"是3的倍數\n"; | |
else | |
cout<<s<<"不是3的倍數\n"; | |
} | |
return 0; | |
} |
# 4 的倍數
末二位數為 4 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int last_two = (s[length-2]-'0')*10+s[length-1]-'0'; | |
if(last_two%4==0) | |
cout<<s<<"是4的倍數\n"; | |
else | |
cout<<s<<"不是4的倍數\n"; | |
} | |
return 0; | |
} |
# 5 的倍數
個位數字為 0 或 5
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
if(s[length-1]=='0'||s[length-1]=='5') | |
cout<<s<<"是5的倍數\n"; | |
else | |
cout<<s<<"不是5的倍數\n"; | |
} | |
return 0; | |
} |
# 6 的倍數
同時是 2 和 3 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
bool is_two = false; | |
if((s[length-1]-'0')%2==0) | |
is_two = true; | |
//------------------------------------ | |
int tmp = 0; | |
for(int i = 0;i<length;i++){ | |
tmp += s[i] - '0'; | |
} | |
bool is_three = false; | |
if(tmp%3==0) | |
is_three = true; | |
//------------------------------------- | |
if(is_two&&is_three) | |
cout<<s<<"是6的倍數\n"; | |
else | |
cout<<s<<"不是6的倍數\n"; | |
} | |
return 0; | |
} |
# 7 的倍數
由個數起每三位數字一節,各奇數節的和與偶數節的和相減,其差是 7 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int tmp_A = 0,tmp_B = 0; | |
for(int i = 0;i<length;i+=6){ | |
int tmp = 0; | |
for(int j = 3;j>0;j--) | |
if(i+j<=length){ | |
tmp = tmp * 10 + s[length-i-j]-'0'; | |
} | |
tmp_A +=tmp; | |
} | |
cout<<tmp_A<<endl; | |
for(int i = 3;i<length;i+=6){ | |
int tmp = 0; | |
for(int j = 3;j>0;j--){ | |
if(i+j<=length){ | |
tmp = tmp * 10 + s[length-i-j]-'0'; | |
} | |
} | |
tmp_B +=tmp; | |
} | |
cout<<tmp_B<<endl; | |
int gap = abs(tmp_A-tmp_B); | |
if(gap%7==0) | |
cout<<s<<"是7的倍數\n"; | |
else | |
cout<<s<<"不是7的倍數\n"; | |
} | |
return 0; | |
} |
# 8 的倍數
末三位數為 8 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int last_three = (s[length-3]-'0')*100+(s[length-2]-'0')*10+s[length-1]-'0'; | |
if(last_three%8==0) | |
cout<<s<<"是8的倍數\n"; | |
else | |
cout<<s<<"不是8的倍數\n"; | |
} | |
return 0; | |
} |
# 9 的倍數
各個數字和為 9 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int tmp = 0; | |
for(int i = 0;i<length;i++){ | |
tmp += s[i] - '0'; | |
} | |
if(tmp%9==0) | |
cout<<s<<"是9的倍數\n"; | |
else | |
cout<<s<<"不是9的倍數\n"; | |
} | |
return 0; | |
} |
# 10 的倍數
個位數字為 0
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
if(s[length-1]=='0') | |
cout<<s<<"是10的倍數\n"; | |
else | |
cout<<s<<"不是10的倍數\n"; | |
} | |
return 0; | |
} |
# 11 的倍數
奇數位數字和與偶數位數字和相差為 11 的倍數。
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int tmp_A = 0; | |
for(int i = 0;i<length;i+=2){ | |
tmp_A +=s[i]-'0'; | |
} | |
int tmp_B = 0; | |
for(int i = 1;i<length;i+=2){ | |
tmp_B +=s[i]-'0'; | |
} | |
int gap = abs(tmp_A-tmp_B); | |
if(gap%11==0) | |
cout<<s<<"是11的倍數\n"; | |
else | |
cout<<s<<"不是11的倍數\n"; | |
} | |
return 0; | |
} |
另一種寫法:
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int tmp_A = 0,tmp_B = 0; | |
for(int i = 0;i<length;i++){ | |
if(i%2==0) | |
tmp_A +=s[i]-'0'; | |
else | |
tmp_B +=s[i]-'0'; | |
} | |
int gap = abs(tmp_A-tmp_B); | |
if(gap%11==0) | |
cout<<s<<"是11的倍數\n"; | |
else | |
cout<<s<<"不是11的倍數\n"; | |
} | |
return 0; | |
} |
# 12 的倍數
同時是 3 和 4 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
//------------------------------------ | |
int tmp = 0; | |
for(int i = 0;i<length;i++){ | |
tmp += s[i] - '0'; | |
} | |
bool is_three = false; | |
if(tmp%3==0) | |
is_three = true; | |
//------------------------------------- | |
int last_two = (s[length-2]-'0')*10+s[length-1]-'0'; | |
bool is_four = false; | |
if(last_two%4==0) | |
is_four = true; | |
if(is_three&&is_four) | |
cout<<s<<"是12的倍數\n"; | |
else | |
cout<<s<<"不是12的倍數\n"; | |
} | |
return 0; | |
} |
# 13 的倍數
由個數起每三位數字一節,各奇數節的和與偶數節的和相減,其差是 13 的倍數
#include<bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int length = s.size(); | |
int tmp_A = 0,tmp_B = 0; | |
for(int i = 0;i<length;i+=6){ | |
int tmp = 0; | |
for(int j = 3;j>0;j--){ | |
if(i+j<=length){ | |
tmp = tmp * 10 + s[length-i-j]-'0'; | |
} | |
} | |
tmp_A +=tmp; | |
} | |
cout<<tmp_A<<endl; | |
for(int i = 3;i<length;i+=6){ | |
int tmp = 0; | |
for(int j = 3;j>0;j--){ | |
if(i+j<=length){ | |
tmp = tmp * 10 + s[length-i-j]-'0'; | |
} | |
} | |
tmp_B +=tmp; | |
} | |
cout<<tmp_B<<endl; | |
int gap = abs(tmp_A-tmp_B); | |
if(gap%13==0) | |
cout<<s<<"是13的倍數\n"; | |
else | |
cout<<s<<"不是13的倍數\n"; | |
} | |
return 0; | |
} |