当前位置: 代码迷 >> C语言 >> [求助]一个ACM的题。
  详细解决方案

[求助]一个ACM的题。

热度:152   发布时间:2006-11-08 16:12:34.0
[求助]一个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  

----------------解决方案--------------------------------------------------------
看起来有点累!不过还是很有收获的!
----------------解决方案--------------------------------------------------------

写的不错,也可以用递归

#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;
}



----------------解决方案--------------------------------------------------------
呵呵!确实简化了不少啊!但是对于很大的数据要怎样处理啊?long型的是不能处理太大的。
----------------解决方案--------------------------------------------------------

有兴趣的看一看吧!呵呵!我现在正在做!
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)


----------------解决方案--------------------------------------------------------

对于大数可以用字符串做

#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;
}



----------------解决方案--------------------------------------------------------
HIT-Online Judge,process result is Okey.
result has 1 wrong answers
为什么还有一个错误啊?
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

第二题

#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;
}



----------------解决方案--------------------------------------------------------
以下是引用zhanghuan_10在2006-11-8 17:09:46的发言:
HIT-Online Judge,process result is Okey.
result has 1 wrong answers
为什么还有一个错误啊?

是不是s[1000]位数还是不够大?
----------------解决方案--------------------------------------------------------

  相关解决方案