單精度和高精度乘法
阿新 • • 發佈:2019-01-03
lightOJ-1024
網上學來的,自己加了註釋便於理解。->http://blog.csdn.net/zyz_3_14159/article/details/52810165#include<cmath> #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int MAXN = 100010; int ans[MAXN], cnt[MAXN], len; int fun(int x,int y)//x代表因子 y代表這個因子出現的次數 { int t=1; for(int i=1; i<=y; i++) t*=x; return t; } void multiply(int fact) { //cout << fact << "**" << endl; for(int i = 0; i < len; i ++) ans[i] *= fact;//先把每位乘以每一個新的因子 for(int i = 0; i < len; i ++)//再重新分解一遍只儲存每個位數 { ans[i+1] += ans[i]/10; ans[i] %= 10; } // for(int i=len;i>=0;i--) // cout << ans[i] <<":"; // cout << endl; int tmp = ans[len]; while(tmp)//分解最後那個數,因為最後可能不是一位數,大數長度增加就是在這裡記錄的 { ans[len++] = tmp%10; tmp /= 10; } // for(int i=len;i>=0;i--) // cout << ans[i] <<"^"; // cout << endl; } int main() { int t, CASE(0), n, tmp; scanf("%d", &t); while(t--) { scanf("%d", &n); memset(cnt, 0, sizeof cnt); memset(ans, 0, sizeof ans); for(int i = 0; i < n; i ++) { scanf("%d", &tmp); int fact = 2; while(tmp != 1)//找出所有的因子,並儲存因子出現的次數 { int num = 0; while(tmp % fact == 0) num ++, tmp /= fact; cnt[fact] = max(cnt[fact], num);//記錄出現次數 fact++; } } ans[0] = len = 1; for(int i = 2; i <= 10000; i ++) if(cnt[i]) multiply(fun(i, cnt[i])); printf("Case %d: ", ++CASE);//上面的迴圈是把所有最高次因子乘起來 for(int i = len - 1; i > 0; i --) printf("%d", ans[i]);//按位輸出大數 printf("%d\n", ans[0]); } return 0; } /* 5 4 3 5 7 11 */