324 - Factorial Frequencies
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=260
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 determine these values.
Recall that the definition of n! (that is, n factorial) is just . 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 ofn, 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
完整代码:
/*0.022s*/ #include<bits/stdc++.h> using namespace std; const int maxn = 800; char outputstr[maxn];///此字符串为bign中s的拷贝,用于输出 int dig[10]; struct bign { int len, s[maxn]; bign() { memset(s, 0, sizeof(s)); len = 1; } bign(int num) { *this = num; } bign(const char* num) { *this = num; } bign operator = (const int num) { char s[maxn]; sprintf(s, "%d", num); *this = s; return *this; } bign operator = (const char* num) { len = strlen(num); for (int i = 0; i < len; i++) s[i] = num[len - i - 1] & 15; return *this; } ///输出 const char* str() const { if (len) { for (int i = 0; i < len; i++) outputstr[i] = '0' + s[len - i - 1]; outputstr[len] = '\0'; } else strcpy(outputstr, "0"); return outputstr; } ///去前导零 void clean() { while (len > 1 && !s[len - 1]) len--; } ///加 bign operator + (const bign& b) const { bign c; c.len = 0; for (int i = 0, g = 0; g || i < max(len, b.len); i++) { int x = g; if (i < len) x += s[i]; if (i < b.len) x += b.s[i]; c.s[c.len++] = x % 10; g = x / 10; } return c; } ///减 bign operator - (const bign& b) const { bign c; c.len = 0; for (int i = 0, g = 0; i < len; i++) { int x = s[i] - g; if (i < b.len) x -= b.s[i]; if (x >= 0) g = 0; else { g = 1; x += 10; } c.s[c.len++] = x; } c.clean(); return c; } ///乘 bign operator * (const bign& b) const { bign c; c.len = len + b.len; for (int i = 0; i < len; i++) for (int j = 0; j < b.len; j++) c.s[i + j] += s[i] * b.s[j]; for (int i = 0; i < c.len - 1; i++) { c.s[i + 1] += c.s[i] / 10; c.s[i] %= 10; } c.clean(); return c; } ///除 bign operator / (const bign &b) const { bign ret, cur = 0; ret.len = len; for (int i = len - 1; i >= 0; i--) { cur = cur * 10; cur.s[0] = s[i]; while (cur >= b) { cur -= b; ret.s[i]++; } } ret.clean(); return ret; } ///模、余 ///本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45518.htm bign operator % (const bign &b) const { bign c = *this / b; return *this - c * b; } bool operator < (const bign& b) const { if (len != b.len) return len < b.len; for (int i = len - 1; i >= 0; i--) if (s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator > (const bign& b) const { return b < *this; } bool operator <= (const bign& b) const { return !(b < *this); } bool operator >= (const bign &b) const { return !(*this < b); } bool operator == (const bign& b) const { return !(b < *this) && !(*this < b); } bool operator != (const bign &a) const { return *this > a || *this < a; } bign operator += (const bign &a) { *this = *this + a; return *this; } bign operator -= (const bign &a) { *this = *this - a; return *this; } bign operator *= (const bign &a) { *this = *this * a; return *this; } bign operator /= (const bign &a) { *this = *this / a; return *this; } bign operator %= (const bign &a) { *this = *this % a; return *this; } } a[367]; int main() { int i, n, len; a[1] = 1; for (i = 2; i < 367; ++i) a[i] = a[i - 1] * i; while (scanf("%d", &n), n) { memset(dig, 0, sizeof(dig)); len = strlen(a[n].str()); for (i = 0; i < len; ++i) ++dig[outputstr[i] & 15]; printf("%d! --\n", n); for (i = 0; i < 10; ++i) { printf(" (%d)%5d", i, dig[i]); if (i == 4) putchar(10); } putchar(10); } return 0; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, return
, this
, const
, operator
, len
outputstring
factorial、factorial函数、factorial design、matlab factorial、factorial label,以便于您获取更多的相关知识。