| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1405 人关注过本帖
标题:hdu问题,Digital Roots
只看楼主 加入收藏
ZJQLOVELYY
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:166
注 册:2011-8-1
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:14 
hdu问题,Digital Roots
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.
 

Sample Input
24
39
0
 

Sample Output
6
3
程序代码:
#include<stdio.h>
int main()
{
    int n,sum=0;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        sum=0;
        while(n!=0)
        {
            sum+=n%10;
            n/=10;
            if(sum>10&&n==0)
            {    
                n=sum;
                sum=0;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
我这个程序为什么通过不了的,,显示Wrong answer
搜索更多相关主题的帖子: single necessary continued positive 
2011-11-22 12:23
jcw08120110
Rank: 8Rank: 8
来 自:南京
等 级:蝙蝠侠
帖 子:272
专家分:742
注 册:2009-6-8
收藏
得分:0 
你应该考虑一下sum的值 这个应该要用到递归!我再你得题目上面改一下!
程序代码:
#include<stdio.h>
int sum;
void f(int n){
    sum+=n%10;
    n/=10;
    if(n>9)
        f(n);
    else
        sum+=n;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        sum=0;
        f(n);
        printf("%d\n",sum);
        while(sum>9)
        {
            int k=sum;
            sum=0;
            f(k);
        }
        printf("%d\n",sum);
    }
    return 0;
}

君生我未生 我生君以老
2011-11-22 14:58
jcw08120110
Rank: 8Rank: 8
来 自:南京
等 级:蝙蝠侠
帖 子:272
专家分:742
注 册:2009-6-8
收藏
得分:0 
互相探讨一下我觉得应该把 sum放进 main 里面~ 而作为函数参数传递 ~ 以减少全局函数~
还有main里面只调用一次f();就能直接输出结果 ~

君生我未生 我生君以老
2011-11-22 15:01
jcw08120110
Rank: 8Rank: 8
来 自:南京
等 级:蝙蝠侠
帖 子:272
专家分:742
注 册:2009-6-8
收藏
得分:1 
程序代码:
#include<stdio.h>
int f(int n,int sum){
    sum+=n%10;
    n/=10;
    if(n>9)
        sum=f(n,sum);
    else
        sum+=n;
    if(sum>9)
    {
        int k=sum;
        sum=0;
        sum+=f(k,sum);
    }
    return sum;
}
int main()
{
    int n,sum;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        sum=0;
        sum=f(n,sum);
        printf("%d\n",sum);
    }
    return 0;
}

君生我未生 我生君以老
2011-11-22 15:12
jcw08120110
Rank: 8Rank: 8
来 自:南京
等 级:蝙蝠侠
帖 子:272
专家分:742
注 册:2009-6-8
收藏
得分:0 
思考了下发现有意思哦~ 题目没要求负数怎么办! 还有没说 要转换的数有没有界限!~ 如果超过了界限 咱就不能用int来表示了!!!

君生我未生 我生君以老
2011-11-22 16:09
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:4 
AC代码
程序代码:
#include <stdio.h>
#include <string.h>
char a[1000];
int main()           
{
    int i,j,k;
    char temp[10];
    while(scanf("%s",a) && a[0]-48)
    {
        int len = strlen(a);
        int sum = 0;
        for(i = 0;a[i];i++)sum+=a[i]-48;
        while(sum>9)
        {
            sprintf(temp,"%d",sum);
            sum = 0;
            len = strlen(temp);
            for(i = 0;temp[i];i++)sum+=temp[i]-48;
        }
        printf("%d\n",sum);
    }
    return 0;
}


                                         
===========深入<----------------->浅出============
2011-11-22 20:30
ZJQLOVELYY
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:166
注 册:2011-8-1
收藏
得分:0 
回复 2楼 jcw08120110
这个程序应该不行,,在输入38的时候,即sum大于9的时候,还是会输出sum,再去计算,,,这样就不符合题意了
2011-11-23 14:04
ZJQLOVELYY
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:166
注 册:2011-8-1
收藏
得分:0 
回复 4楼 jcw08120110
这个也过不了!
2011-11-23 14:17
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
我的能过

                                         
===========深入<----------------->浅出============
2011-11-23 16:40
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:0 
用一个函数,比如int fun (int n),当输入n小于等于9时,返回n,输入n大于9时,把n的每一位都加起来再调用fun函数,知道所有位数的和为个位数为止,具体实现如下:
程序代码:
int fun(int n)                        //函数保证返回值是基本数字
{
    int r = 0;
    if (n <= 9)
        return n;

    while (n > 0)
    {
       r += n%10;
       n /= 10;
    }
    return fun(r);
}
主函数中处理输入,把输入转换成一个整数n再进行函数fun的调用,基本上思想都这样。


冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2011-11-23 16:52
快速回复:hdu问题,Digital Roots
数据加载中...
 
   



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

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