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

HDU 4722 WA求大神指教(已经更正)

2013年10月31日 ⁄ 综合 ⁄ 共 840字 ⁄ 字号 评论关闭

           规律很容易发现,从10开始,每10个数,有一个满足条件。即19,28,37,46,55,64,73,82,91,109,118,127,136,145,154,163,,,,,,,

          代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
int main()
{
//  freopen("in.txt","r",stdin);
    int t;
    LL num1,num2;
    scanf("%d",&t);
    for(int cas=1; cas<=t; cas++)
    {
        scanf("%I64d%I64d",&num1,&num2);
        LL  cnt=0;
//        if(num1<19)
//            num1=18;
//        if(num2<19)
//            num2=18;//我的代码就错在了这4行语句上,因为19之前的数没有满足条件的,所以直接把19以下的扩大到了18,不知道为何WA了。去掉这四行,就AC了。
        LL l=num1,r=num2;
        while(l%10!=0)
            l++;
        while(r%10!=0)
            r--;
        if(r<=l)
        {
            for(LL i=num1; i<=num2; i++)
            {
                LL  x=i;
                LL ans=0;
                while(x)
                {
                    ans+=x%10;
                    x/=10;
                }
                if(ans%10==0)
                    cnt++;
            }
        }
        else
        {
            for(LL i=num1; i<l; i++)
            {
                LL  x=i;
                LL  ans=0;
                while(x)
                {
                    ans+=x%10;
                    x/=10;
                }
                if(ans%10==0)
                {
                    cnt++;
                    break;
                }
            }
            for(LL i=num2; i>=r; i--)
            {
                LL x=i;
                LL ans=0;
                while(x)
                {
                    ans+=x%10;
                    x/=10;
                }
                if(ans%10==0)
                {
                    cnt++;
                    break;
                }
            }
            cnt+=(r-l)/10;
        }
        printf("Case #%d: %I64d\n",cas,cnt);
    }
    return 0;
}

抱歉!评论已关闭.