• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

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 }
posted @ 2012-05-15 09:55  Hug_Sea  阅读(791)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3