| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2118 人关注过本帖
标题:[求助]一个ACM的题。
取消只看楼主 加入收藏
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
 问题点数:0 回复次数:12 
[求助]一个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
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
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
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
我改一下试一试!
呵呵!在这里恭喜你成为版主了!!!

该学习了。。。
2006-11-08 17:36
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
呵呵!谢谢版主了!问题果然出在了这里。

该学习了。。。
2006-11-08 17:39
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
版主第二题好像做的不对吧!
scanf("%f",&num)!=EOF好像是当按F6时程序就会结束!

该学习了。。。
2006-11-08 17:49
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
呵呵!是我弄错了!版主作的是对的!

该学习了。。。
2006-11-08 18:06
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
while(s[i]!='\0')
num+=s[i++]-'0';
这是什么意思啊?

该学习了。。。
2006-11-08 18:15
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
哦。明白了!用的好巧妙啊!应该好好学学呀!

该学习了。。。
2006-11-08 18:40
快速回复:[求助]一个ACM的题。
数据加载中...
 
   



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

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