国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

1059. Prime Factors (25)

2019-11-10 17:41:56
字體:
供稿:網(wǎng)友

Given any positive integer N, you are supposed to find all of its PRime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = p1^k1 * p2^k2 *…*pm^km, where pi’s are prime factors of N in increasing order, and the exponent ki is the number of pi – hence when there is only one pi, ki is 1 and must NOT be printed out.

Sample Input: 97532468 Sample Output: 97532468=2^2*11*17*101*1291

#include<cstdio>#include<cmath>const int maxn=100010;//因?yàn)轭}目說在int范圍內(nèi)(10^9)的正整數(shù)進(jìn)行質(zhì)因子分解,因此素?cái)?shù)表大概開10^5就可以了 bool isPrime(int x){ bool flag=true; if(x<=1) flag=false; for(int i=2;i*i<=x;i++){ if(x%i==0){ flag=false; break; } } return flag;}int prime[maxn],pNum=0;void findPrime(){ for(int i=2;i<maxn;i++){ if(isPrime(i)){ prime[pNum++]=i; } }}//本來想用個(gè)HashTable[],下標(biāo)存放質(zhì)因子,值存其個(gè)數(shù),如HashTable[5]=4,即4個(gè)5,//但輸出時(shí)不方便,必須開一個(gè)int范圍的數(shù)組(n=10^9),且要遍歷全部元素, struct factor{ int x,cnt;//x為質(zhì)因子,x為其個(gè)數(shù) }fac[10];//int型(10^9)范圍內(nèi)的數(shù),不同質(zhì)因子數(shù)不可能超過10個(gè), //因?yàn)閷?duì)于x=2*3*5*7*11*13*17*19*23*29,x有十個(gè)質(zhì)因子,x已超出int范圍,即一個(gè)數(shù)若有十個(gè)不同質(zhì)因子,它一定大于等于x,超出int范圍 int main(){ findPrime(); int n,num=0;//num為n的不同質(zhì)因子個(gè)數(shù) scanf("%d",&n); if(n==1) printf("1=1/n"); else{ printf("%d=",n); int sqr=(int)sqrt(n); for(int i=0;i<pNum&&prime[i]<=sqr;i++){ if(n%prime[i]==0){ fac[num].x=prime[i]; fac[num].cnt=0; while(n%prime[i]==0){ fac[num].cnt++; n/=prime[i]; } num++; } if(n==1) break; } if(n!=1){//如果無法被根號(hào)n以內(nèi)的質(zhì)因數(shù)除盡,如38=2*19,根號(hào)n為6<x<7,循環(huán)結(jié)束時(shí)n=19 fac[num].x=n; fac[num++].cnt=1; } for(int i=0;i<num;i++){ printf("%d",fac[i].x); if(fac[i].cnt>1){ printf("^%d",fac[i].cnt); } if(i<num-1) printf("*"); } } return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安泽县| 盐亭县| 峨眉山市| 瑞昌市| 通江县| 安陆市| 蕉岭县| 肥乡县| 尤溪县| 西平县| 大渡口区| 门头沟区| 江阴市| 皋兰县| 河北区| 娄烦县| 西吉县| 昌宁县| 崇左市| 靖安县| 金山区| 汝城县| 敖汉旗| 广元市| 博乐市| 台北市| 宾川县| 香港| 广东省| 循化| 安乡县| 济阳县| 育儿| 莎车县| 堆龙德庆县| 通榆县| 东莞市| 徐汇区| 昆明市| 万州区| 怀远县|