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

 

View Code
 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 }

转载于:https://www.cnblogs.com/Hug-Sea/articles/2500977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值