Check if a large number is divisible by 3 or not

 1 //检验一个大数是否能3整除
 2 //A number is divisible by 3 if sum of its digits is divisible by 3.
 3 //we cannot use n % 3 to check if a number is divisible by 3 or not.
 4 //Remainder of 10i divided by 3 is 1 So powers of 10 only result in value 1.
 5 #include<bits/stdc++.h>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     string s;
11     cin>>s;
12     int len=s.length();
13     int sum=0;
14     for(int i=0;i<len;i++)
15     {
16         sum+=s[i]-'0';
17     }
18     if(sum%3==0)
19         cout<<"yes"<<endl;
20     else
21         cout<<"no"<<endl;
22     return 0;
23 }
1 # “脱数“代码
2 def check(num):
3     while num>0:
4         rem=num%10
5         sum+=rem
6         num/=10

猜你喜欢

转载自www.cnblogs.com/chuanwen-tech/p/11296178.html