1136 - Division by 3
题意:There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Now you are given two integers A and B, you have to find the number of integers from Ath number to Bth (inclusive) number, which are divisible by 3.
For example, let A = 3. B = 5. So, the numbers in the sequence are, 123, 1234, 12345. And 123, 12345 are divisible by 3. So, the result is 2.
思路:找到规律,1 0 0 1 0 0 1 0 0......(1不能被3整除,0能被3整除),然后就是细节问题,但还是w了好几次.......- -!
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1136
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <string> 5 #include <algorithm> 6 #include <iostream> 7 using namespace std; 8 9 int main(){ 10 11 // freopen("data.in","r",stdin); 12 // freopen("data.out","w",stdout); 13 14 int t,a,b,i; 15 scanf("%d",&t); 16 for(i=1;i<=t;i++){ 17 scanf("%d%d",&a,&b); 18 int sum=0; 19 if(a%3!=1){ 20 if(a%3==2){sum+=2;a+=2;} 21 else {sum++;a++;} 22 } 23 if(b%3!=1){ 24 if(b%3==2) {sum++;b--;} 25 else {sum+=2;b-=2;} 26 } 27 printf("Case %d: %d\n",i,sum+b-a-(b-a)/3); 28 } 29 return 0; 30 }