| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 452 人关注过本帖
标题:hdu1013 wrong answer 怎么回事? 觉得考虑的很周到了
只看楼主 加入收藏
清尘J
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2013-2-17
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:7 
hdu1013 wrong answer 怎么回事? 觉得考虑的很周到了
#include<stdio.h>
int main(){
    int n;
    int m;
    scanf("%d",&n);
    while(n){
        m=0;
        while(n>=10||m>=10){
            while(n>=10){
                 m+=n%10;
                n/=10;
            }
            m+=n;
            if(m>=10)
                 n=0;
            while(m>=10){
                n+=m%10;
                m/=10;
            }
            n+=m;
            if(n>=10)
                m=0;
        }
        printf("%d\n",n>m?n:m);
        scanf("%d",&n);
    }
    return 0;
}
搜索更多相关主题的帖子: include wrong 
2013-02-21 14:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:2 
hdu1013 是什么?与其让每一个看你贴的人去重复Google,不如你直接贴出来以节约能源。
2013-02-21 14:55
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:2 
发个问题连题目都没有,还说自己考虑周到

总有那身价贱的人给作业贴回复完整的代码
2013-02-21 14:59
清尘J
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2013-2-17
收藏
得分:0 

Problem Description
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

 

Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.

 

Output
For each integer in the input, output its digital root on a separate line of the output.

 抱歉了  题目没弄

2013-02-21 15:22
清尘J
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2013-2-17
收藏
得分:0 
回复 3楼 embed_xuel
抱歉了  题目没贴出来
因为刚用这个
没有太多经验
现在贴出来了
求指导一下
2013-02-21 15:25
清尘J
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2013-2-17
收藏
得分:0 
回复 2楼 rjsp
抱歉了  题目没贴出来
因为刚用这个
没有太多经验
现在贴出来了
求指导一下
2013-02-21 15:25
czz5242199
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:660
专家分:2400
注 册:2011-10-26
收藏
得分:2 
程序代码:
#include <stdio.h>

char c;
int ans=0;

int main()
{
    while (scanf("%c",&c))
    {
          if (ans==0 && c=='0') break; 
          else if (c=='\n')
          {
                      ans%=9;
                      if (ans==0) ans=9;
                      printf("%d\n",ans);
                      ans=0;
          }
          else ans+=c-'0';
    }
}
    
数字可能会很长
2013-02-21 16:25
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:4 
楼主不得不说你几句 你的代码确实写的太乱 不是你考虑的周不周到的问题 这样的逻辑 考虑的再周到也无济于事
用 n m 两个变量来迭代循环 这个思路是对的 但是不是这样迭代的
要找你的代码的具体漏洞是有的 比如计算13 在m = 4 的时候 已经小于10了
但是跑都后面的时候 出现了一条不加任何限制就执行的 n+=m 使得原来n为1 加上4变为5 所以最后输出5 这就是一个结果错误的例子
我建议你不要试着去维护你的这段代码了 虽然是你的成果 下面这段代码 希望能给你参考 如果有错误 请指正
程序代码:
#include<stdio.h>

int main(){
    int m, n;
    
    do{
        scanf("%d", &n);
        if(n){
            while(n >= 10){
                m = 0;
                while(n){
                    m += n%10;
                    n /= 10;
                }
                n = m;
            }
            printf("%d\n", n);    
        }
        else break;        
    }while(1);
    
    return 0;
}

人生是一场错过 愿你别蹉跎
2013-02-21 17:27
快速回复:hdu1013 wrong answer 怎么回事? 觉得考虑的很周到了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015485 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved