數字的質因子分解。。
Code:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long LL; int main() { LL n, p, t, i; int pri[100], e[100]; while (true) { cin >> p; n = 1; while (0 != p) { cin >> t; n *= pow(p, t); if (getchar() == '\n') break; cin >> p; } if (p == 0) break; int cnt = 0; --n; // printf("%d\n",n); int k = 2; memset(e, 0, sizeof(e)); while (k <=sqrt(n)) { if (n % k == 0) { pri[cnt] = k; while (n % k == 0) { e[cnt]++; n /= k; } // printf("%d %d\n",pri[cnt], e[cnt]); cnt++; } if (k != 2) k += 2; else k++; } if(n>1) { pri[cnt] = n; e[cnt]++; cnt++; } for (i = cnt-1; i >0; i--) printf("%d %d ", pri[i], e[i]); printf("%d %d\n",pri[0],e[0]); } return 0; } #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long LL; int main() { LL n, p, t, i; int pri[100], e[100]; while (true) { cin >> p; n = 1; while (0 != p) { cin >> t; n *= pow(p, t); if (getchar() == '\n') break; cin >> p; } if (p == 0) break; int cnt = 0; --n; // printf("%d\n",n); int k = 2; memset(e, 0, sizeof(e)); while (k <=sqrt(n)) { if (n % k == 0) { pri[cnt] = k; while (n % k == 0) { e[cnt]++; n /= k; } // printf("%d %d\n",pri[cnt], e[cnt]); cnt++; } if (k != 2) k += 2; else k++; } if(n>1) { pri[cnt] = n; e[cnt]++; cnt++; } for (i = cnt-1; i >0; i--) printf("%d %d ", pri[i], e[i]); printf("%d %d\n",pri[0],e[0]); } return 0; }