| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2119 人关注过本帖
标题:[求助]一个ACM的题。
只看楼主 加入收藏
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
 问题点数:0 回复次数:26 
[求助]一个ACM的题。

我已在 “建议提供练习题 ”发表了这个题!但是我想问一下要给一个很大的数据要如何处理呢?
Digital Roots

Background
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.

Example
Input
24
39
0

Output
6
3

这是我写的代码!写的不好!请指教一下!对于很大的数据要如何来处理啊!谢谢了!
#include <stdio.h>
#include <stdlib.h>

int main()
{
long num, root;

scanf("%ld", &num);
while(num != 0)
{
long sum_integer(long num);

root = sum_integer(num);
while(root > 10)
{
root = sum_integer(root);
}
printf("%ld\n", root);
scanf("%ld", &num);
}

return 0;
}

long sum_integer(long num)
{
int i;
long g = 0;

for(i = 0;;i ++)
{
if(num < 10)
break;
g += num%10;
num = num/10;
}

g += num;

return g;
}

搜索更多相关主题的帖子: ACM digit integer root single 
2006-11-08 16:12
wfd2004
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2006-11-7
收藏
得分:0 
看起来有点累!不过还是很有收获的!

在交流中成长
2006-11-08 16:27
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 

写的不错,也可以用递归

#include <stdio.h>

long digital_root(long num)
{
long sum=0;
if (num<10)
return num;
while(num!=0)
{
sum+=num%10;
num/=10;
}
return digital_root(sum);
}

int main()
{
long num;
while(EOF!=scanf("%ld",&num) && num!=0)
printf("%ld\n",digital_root(num));
return 0;
}



2006-11-08 16:31
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
呵呵!确实简化了不少啊!但是对于很大的数据要怎样处理啊?long型的是不能处理太大的。

该学习了。。。
2006-11-08 16:42
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 

有兴趣的看一看吧!呵呵!我现在正在做!
HangOver
How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.

The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.

Sample Input
1.00
3.71
0.04
5.19
0.00

Sample Output
3 card(s)
61 card(s)
1 card(s)
273 card(s)


该学习了。。。
2006-11-08 16:51
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 

对于大数可以用字符串做

#include <stdio.h>
#include <string.h>
long digital_root(long num)
{
long sum=0;
if (num<10)
return num;
while(num!=0)
{
sum+=num%10;
num/=10;
}
return digital_root(sum);
}

int main()
{
char s[1000];
long num;
int i;
while(1)
{
gets(s);
if(strcmp(s,"0")==0)
break;
num=0;
i=0;
while(s[i]!='\0')
num+=s[i++]-'0';
printf("%ld\n",digital_root(num));
}
return 0;
}



2006-11-08 16:59
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
HIT-Online Judge,process result is Okey.
result has 1 wrong answers
为什么还有一个错误啊?

该学习了。。。
2006-11-08 17:09
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 

你题目中没有明确指明范围的限制
以及输入的形式啊


2006-11-08 17:14
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 

第二题

#include <stdio.h>
int minimum(float num)
{
int i=1;
float s=0.0;
while(s<num)
s+=1.0f/++i;
return i-1;
}

int main()
{
float num;
while(scanf("%f",&num)!=EOF && num>0.000001)
printf("%d card(s)\n",minimum(num));
return 0;
}



2006-11-08 17:15
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 
以下是引用zhanghuan_10在2006-11-8 17:09:46的发言:
HIT-Online Judge,process result is Okey.
result has 1 wrong answers
为什么还有一个错误啊?

是不是s[1000]位数还是不够大?


2006-11-08 17:26
快速回复:[求助]一个ACM的题。
数据加载中...
 
   



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

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