Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
题意模糊不清!输入中是质数和指数一起显示的!比如509,1,59,1表示509^1*59^1,而不是7^509*5^1*3^59*2^1! (要是后者这题该怎么做啊2333) 附代妈 #include <iostream> #include <stdio.h> #include <sstream> #include <string> using namespace std; int primes[10000]; int primeCnt; bool isPrime(int n){ for(int i = 1; i < primeCnt; i++){ if(primes[i]*primes[i]>n) break; if(n%primes[i] == 0) return 0; } return 1; } void init(){ primes[0] = 2; primeCnt = 1; for(int i = 3; i <= 32767; i+=2){ if(isPrime(i)){ primes[primeCnt] = i; primeCnt++; } } } int main() { init(); string s; while(1){ getline(cin, s); if(s[0] == '0') break; stringstream ss(s); int n = 1; int p, a; while(ss >> p){ ss >> a; for(int j = 0; j < a; j++) n *= p; } n--; int fjgs = 0; int ps[50], ms[50]; for(int i = 0; i < primeCnt; i++){ if(primes[i]*primes[i] > n) break; int pss=primes[i], mss=0; while(n%pss==0){ n/=pss; mss++; } if(mss>0){ ps[fjgs]=pss; ms[fjgs]=mss; fjgs++; } } if(n>1){ ps[fjgs]=n; ms[fjgs]=1; fjgs++; } for(int j = fjgs-1; j>=0; j--){ printf("%d %d ", ps[j], ms[j]); } printf("\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator