日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

信息学奥赛一本通 1172:求10000以内n的阶乘 | OpenJudge NOI 1.6 14:求10000以内n的阶乘

發布時間:2025/3/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 信息学奥赛一本通 1172:求10000以内n的阶乘 | OpenJudge NOI 1.6 14:求10000以内n的阶乘 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【題目鏈接】

ybt 1172:求10000以內n的階乘
OpenJudge NOI 1.6 14:求10000以內n的階乘

【題目考點】

1. 高精度

考察:高精乘低精
高精度計算講解

【解題思路】

先求結果最大的位數,也就是10000!10000!10000!的位數。
已知某正整數x的的位數為n,那么有:n=?lgx?+1n = \lfloor lgx \rfloor + 1n=?lgx?+1lgxlgxlgx:以10為底x的對數)
位數為?lg10000!?+1≤?lg1000010000?+1=?10000?lg10000?+1=?10000?4?+1=40001\lfloor lg10000! \rfloor + 1\le \lfloor lg10000^{10000} \rfloor + 1=\lfloor 10000\cdot lg10000 \rfloor + 1=\lfloor 10000*4 \rfloor + 1 = 40001?lg10000!?+1?lg1000010000?+1=?10000?lg10000?+1=?10000?4?+1=40001
數字數組長度取40005即可。
由于求10000的階乘,每次乘的數字都是小于等于10000的數字,為低精度數字。階乘的結果為高精度數字。所以應該使用高精乘低精。如果使用高精乘高精,算法復雜度會增大,可能會超時。

【題解代碼】

解法1:使用函數與數組

#include <bits/stdc++.h> using namespace std; #define N 40005 void setLen(int a[], int i) {while(a[i] == 0 && i > 1)//去除多余的0i--;a[0] = i; } void Multiply(int a[], int b)//a *= b 高精乘低精 {int c = 0, i;for(i = 1; i <= a[0]; ++i){a[i] = a[i]*b + c;c = a[i] / 10;a[i] %= 10; }while(c > 0){a[i] = c % 10;c /= 10;i++;}setLen(a, i); } void toNum(char s[], int a[]) {a[0] = strlen(s);for(int i = 1; i <= a[0]; ++i)a[i] = s[a[0] - i] - '0'; } void showNum(int a[]) {for(int i = a[0];i >= 1;--i)cout << a[i]; } int main() {int a[N] = {1, 1}, n;//高精度數字a初值為1 cin >> n;for(int i = 1; i <= n; ++i)Multiply(a, i);showNum(a);return 0; }

解法2:類中重載運算符

#include <bits/stdc++.h> using namespace std; #define N 40005 struct HPN {int a[N];//數字數組HPN(){memset(a, 0, sizeof(a));}HPN(char s[]){memset(a, 0, sizeof(a));int len = strlen(s);for(int i = 0; i < len; ++i)a[len - i] = s[i] - '0';a[0] = len;}void setLen(int i){while(a[i] == 0 && i > 1)//去除多余的0i--;a[0] = i;}void operator *= (int b)//a *= b 高精乘低精{int c = 0, i;for(i = 1; i <= a[0]; ++i){a[i] = a[i]*b + c;c = a[i] / 10;a[i] %= 10; }while(c > 0){a[i] = c % 10;c /= 10;i++;}setLen(i);}void show(){for(int i = a[0]; i >= 1; --i)cout << a[i];} }; int main() {HPN a("1");//高精數字a初值為1 int n;cin >> n;for(int i = 1; i <= n; ++i)a *= i;//高精乘低精a.show();return 0; }

總結

以上是生活随笔為你收集整理的信息学奥赛一本通 1172:求10000以内n的阶乘 | OpenJudge NOI 1.6 14:求10000以内n的阶乘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。