现在的位置: 首页 > 综合 > 正文

hdu 短

2018年01月21日 ⁄ 综合 ⁄ 共 1022字 ⁄ 字号 评论关闭
文章目录

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 172   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

输出[L, R]中能被9整除的数的个数

Input

多组测试数据
每组测试数据包含两个数L和R
所有数据都在int范围内。

Output

每组数据输出一个整数,表示区间[L, R]中能被9整除的数的个数

Sample Input

1 9
1 10

Sample Output

1
1

/*题解:
    有点略坑的题,考虑完所有的情况再Wrong了一次的情况下A了。
注意点:
    输入数据为int范围内的整数,所以可以是负数,例如-18、-19 
    输入数据不一定L<=R,所以先判断大小并交换位置。(L<=R则不用交换) 
    0也可以被9整除
    【0,0】,【9,9】,【-9,-9】能被整除个数均为1
    提供几组数据:
        【0,19】 3
        【9,18】 2
        【-18,-9】 2
        【-20,9】 4 
        【-18,-7】 2 
*/

#include<cstdio>
#include<cmath>
#include<stdlib.h>
int main(){
    int l,r,t;
    while(scanf("%d %d",&l,&r)!=EOF)
    {
        if(l>r)//不交换会Output Limit Exceeded 
        {
            t=l;
            l=r;
            r=t;
        }
        if(l==r)//情况一、l==r 
        {
            if(l%9==0)
            {
                printf("1\n");
            }
            else printf("0\n");
        }
        else
        {
            if(l>0)//l>0,r>0 
            {
                printf("%d\n",r/9-(l-1)/9);
            }
            else if(l==0)//l==0,r>0
            {
                printf("%d\n",r/9+1);
            }
            else if(l<0)
            {
                if(r==0)//l<0,r==0 
                {
                    printf("%d\n",abs(l/9)+1);
                }
                else if(r<0)//l<0,r<0
                {
                    printf("%d\n",abs(l/9)-(abs(r)-1)/9);
                }
                else if(r>0)//l<0,r>0
                {
                    printf("%d\n",r/9+abs(l/9)+1);
                }
            }
        }
    }
    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.