题意: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了好几次.......- -!
题目链接:
View Code
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 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 }