| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 502 人关注过本帖
标题:帮忙找下有什么问题。。郁闷死!
只看楼主 加入收藏
wbq777
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-6-24
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:8 
帮忙找下有什么问题。。郁闷死!
一个求数字字根的题:原题:
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.

输入提示:
24
39
0
对应的输出提示:
6
3
我的程序有两个,,但是在杭电上面都是出现Wrong Answer..
1:
#include"stdio.h"
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        if(n%9==0)
        {
            printf("%d\n",9);
        }
        else
        {
            printf("%d\n",n%9);
        }
    }
}
----------------------------------------
#include"stdio.h"
int main()
{
    int n,a,c,i;
    int b[100];
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
st:
        c=n;
        for(i=1;;i++)
        {
            c=c/10;
            if(c==0)
                break;
        }
        a=i;
        for(i=0;i<a;i++)
        {
            b[i]=n%10;
            n=n/10;
        }
        for(i=0;i<a;i++)
        {
            c=c+b[i];
        }
        if(c<10)
            printf("%d\n",c);
        else if(c==10)
            printf("%d\n",1);
        else if(c>10)
        {
            n=c;
            goto st;
        }
    }
}   

搜索更多相关主题的帖子: single necessary continued positive 
2011-07-31 12:25
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:7 
有没有中文

                                         
===========深入<----------------->浅出============
2011-07-31 15:13
a9517495424
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:305
专家分:139
注 册:2011-7-20
收藏
得分:7 
题目都是英文的...
2011-07-31 20:06
wbq777
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-6-24
收藏
得分:0 
中文啊、、、
题目的意思就是。。24   字根就是 2+4=6,,如果得出来的是9以下的,,那么就结束了输出。以上的就继续做,,比如39  字根是  3+9=12  不行。在做  1+2=3,,所以3就是字根
2011-08-03 19:04
wbq777
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 3楼 a9517495424
中文啊、、、
题目的意思就是。。24   字根就是 2+4=6,,如果得出来的是9以下的,,那么就结束了输出。以上的就继续做,,比如39  字根是  3+9=12  不行。在做  1+2=3,,所以3就是字根
2011-08-03 19:04
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:7 
改成 unsigned int 试试?

[ 本帖最后由 voidx 于 2011-8-3 19:21 编辑 ]
2011-08-03 19:19
wbq777
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 6楼 voidx
貌似没区别吧、、我试了下。。没有用的
2011-08-04 18:47
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<string.h>
int root(int n){
    int sum=0;
    while(n){
        sum+=n%10;
        n/=10;
    }
    return sum;
}
int main(){
    int s,i;
    char n[1001];
    while(scanf("%s",n)!=EOF){
        if(!strcmp(n,"0")) break;
        for(i=0,s=0;n[i]!='\0';i++)
            s+=(n[i]-'0');
        while(s>9)
            s=root(s);
        printf("%d\n",s);
    }
    return 0;
}

很巧妙

                                         
===========深入<----------------->浅出============
2011-08-08 16:27
wbq777
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 8楼 laoyang103
A了。。好厉害。。!谢谢!谢谢。。我要研究研究!
2011-08-09 13:16
快速回复:帮忙找下有什么问题。。郁闷死!
数据加载中...
 
   



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

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