码迷,mamicode.com
首页 > 其他好文 > 详细

Check if a large number is divisible by 3 or not

时间:2019-08-03 21:58:36      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:pow   cannot   c++   not   while   its   ble   clu   检验   

 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

 

Check if a large number is divisible by 3 or not

标签:pow   cannot   c++   not   while   its   ble   clu   检验   

原文地址:https://www.cnblogs.com/chuanwen-tech/p/11296178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!