Factorial Frequencies

http://acm.hit.edu.cn/hoj/problem/view?id=1241



 Factorial Frequencies
My Tags (Edit)
Source : UVA - V3
Time limit : 1 secMemory limit : 32 M
Submitted : 362, Accepted : 172
In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to detemine these values.
Recall that the definition of n! (that is, n factorial) is just  tex2html_wrap_inline28 . As she expects to use either the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.
Input and Output
The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don't bother to process this zero value; just stop your program at that point. The output format isn't too critical, but you should make your program produce results that look similar to those shown below.
Madam Phoenix will be forever (or longer) in your debt; she might even give you a trip if you do your job well!
Sample Input
3
8
100
0
Sample Output
3! --
   (0)    0    (1)    0    (2)    0    (3)    0    (4)    0
   (5)    0    (6)    1    (7)    0    (8)    0    (9)    0
8! --
   (0)    2    (1)    0    (2)    1    (3)    1    (4)    1
   (5)    0    (6)    0    (7)    0    (8)    0    (9)    0
100! --
   (0)   30    (1)   15    (2)   19    (3)   10    (4)   10
   (5)   14    (6)   19    (7)    7    (8)   14    (9)   20








好吧,题目很明显提示了得到结果数字位数有781,肯定超过double,long型的存储,很自然的想用字符串处理。但是用字符串怎么处理呢?
题目大意:输入365以内的数字n,统计n!的结构中各个数字出现频次。


用字符串存储最后的计算结果,因为最大为781位数,所以定义一个781个字符的字符串,分别存储每一位存放的数字,初始化为0;


根据多位数乘法规则,应该乘数的每一位去乘被乘数,再将所有的乘积加起来便是最终结果。




这里将上次得到的结果当成乘数,每次阶乘的数为被乘数,将每次得到的乘积放在一开始定义的digit字符串组里面。这边不忙着进位,因为一进位乘数就变了,会影响结果,用已经乘过的数位存储计算结果,最后将结果进位整理。




得到计算结果再进行统计。


#include "stdafx.h"


int main()
{

int num;
while (scanf_s("%d", &num) && (num != 0))
{
int digit[781], count[10];
for (int i = 0; i < 10; i++)
count[i] = 0;
for (int i = 0; i < 781; i++)
digit[i] = 0;
digit[0] = 1;


for (int i = 2; i <= num; i++)
{
for (int j = 0; j < 781; j++)
{
digit[j] *= i;
}
for (int j = 0; j < 781; j++)
{
if (digit[j]>10)
{
digit[j + 1] += digit[j] / 10;
digit[j] = digit[j] % 10;
}
}
}
int tmp = 780;
while (1 == 1)
{
if (digit[tmp] == 0)
tmp--;
else
break;
}
while (tmp != -1)
{
count[digit[tmp]]++;
//printf("%d", digit[tmp]);
tmp--;
}
//printf("\n");
double tmp1 = 1;
for (int i = 1; i <=num; i++)
{
tmp1 =tmp1* i;
}
//printf("num=%f\n", tmp1);
printf("%d! --\n", num);
for (int i = 0; i < 10; i++)
{
if (i != 0 && i != 5)
{
printf(" ");
}
printf("   (%d)%5d", i, count[i]);
if (i == 4)
printf("\n");
}
printf("\n");






}

return 0; }






posted @ 2015-02-15 01:20  aprilvkuo  阅读(243)  评论(0编辑  收藏  举报