HDU-1013 网上的前辈给我的明灯

9 篇文章 0 订阅
8 篇文章 0 订阅

此题本来以为很水.多次WA不过.先贴一遍最初的版本:

#include <stdio.h>
#include <iomap.h>
/*
Problem 1013:Digital Roots
Version :2012-8-11
Author:Unibrighter
Tips:Use the idea of Recursion
*/
int sumDig(int n)
{
    if(n<10)
        return n;
    else
    {
        int sum=0;
        while(n)//if n can still carry a dig
        {
            sum+=(n%10);
            n/=10;//Remeber if the result <0,then we get n as 0(e.g. n=8,n/=10;then n=0)
        }
        return sumDig(sum);
    }
}
int main()
{
    freopen(".\\1013in.txt","r",stdin);//   Redirection of the Input Stream and get the Test Statics
    int num,res;
    while((scanf("%d",&num)!=EOF)&&num)
    {
        printf("%d\n",sumDig(num));
    }
    return 0;
}

一直WA,在Discusion版块里面看了帖子,跟前人有一样的感受,特么竟然不说说数据长度和n的大小!谁知道你要的是大整数?!恶心!

"恶心,恶心哪!我都关着灯!"


接着是AC的代码:

#include <stdio.h>
/*
Author:Unibrighter
Version:2012-8-13
Problem ID:HDU 1013 Digital Root
Note:Get some idea from Jopix Blog(陈侠 暨南大学)
http://blog.sina.com.cn/s/blog_6ec19c780100vcn2.html

What really made me embarrased is that I made the same mistake as he did.(╯﹏╰)b
*/
int main()
{
    char tmp;
    int i,sum;
    //freopen(".\\1013in.txt","r",stdin);
    while(1)
    {
        sum=0;
        for(i=0;i<1000;i++)
        {
            scanf("%c",&tmp);
            if(tmp=='\n')
                break;
            sum+=(tmp-'0');
        }
        if(sum==0)
            break;
        printf("%d\n",sum%9==0?9:sum%9);
    }
    return 0;
}

开始也是一直AC,后才参考了 Jofix 的代码和Tips.这才AC,一样没有严谨的考虑到9取模为0.他大爷的.还是自己的底子不够厚实么...

多看看别人的Blog.差距啊啊.还有Discuss版块也是学习的好地方!


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值