当前位置:网站首页>Prime factorization -- C (GCC) -- PTA
Prime factorization -- C (GCC) -- PTA
2022-07-27 03:47:00 【Zero dimensional expansion Zhizi】
Given a positive integer N, Find the prime factorization result , That is, the factorization expression is given N=p1k1⋅p2k2⋯pmkm.
【 Input format 】
Input long int Positive integer in range N
【 Output format 】
Output in the given format N Prime factorization expression of , namely N=p1^k1*p2^k2*…*pm^km, among pi It is a prime factor and requires small to large output , Index ki by pi The number of ; When ki by 1 That's the factor pi No output when there is only one ki.
【 sample input 】
1323
【 sample output 】
1323=3^3*7^2
Code length limit
16 KB
The time limit
400 ms
Memory limit
64 MB
【 Thinking simulation 】
First step : If the prime factor grows from small to large , We can set an initial value n=2, Every time you judge n Prime or not , If not n++, If yes, then judge whether it is N Factor of .
The second step : take n The value is assigned to i, Let the index initially be k=0, because n Only the external cycle can change ,i accord with N After the factor bar of , Make N/=i, When N=0 When, the external loop terminates , Represents that the expression terminates , Judge i Whether it appears for the first time , The first occurrence will i Assign to new variable i1, The purpose of doing this is i1=i when , Just give it to k++ Represents that the latter factor is the same as the previous factor , Index ++,i1 It's not equal to i when , Represents the emergence of new factors , Output the current expression , At this time k=1, Represents the initial value of the new factor .
#include<stdio.h>
#include<math.h>
int main(){
long int N,n=2,i=0,i1=0,j=2;//i Is a prime factor ,,i1 Is the coefficient ,k Is the index
int k=0;
scanf("%ld",&N);
printf("%ld=",N);
if(N==1){
printf("1");
return 0;
}
while(N!=1){
i=n;
for(j=2;j<=sqrt(i);j++){
if(i%j==0){
break;
}
}
if(j<=sqrt(i)){
n++;
continue;
}
if(N%i==0){
N/=i;
if(k==0){
i1=i;
}
if(i==i1){
k++;
}else{
if(k==1){
printf("%ld*",i1);
}else{
printf("%ld^%d*",i1,k);
}
k=1;
}
i1=i;
}else{
n++;
}
if(N==1){
if(k==1){
printf("%ld",i);
}else{
printf("%ld^%d",i,k);
}
}
}
return 0;
}边栏推荐
猜你喜欢

app端接口用例设计方法和测试方法

Basic concept and essence of Architecture
![[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree](/img/b3/58c053d082807a0e5639e5b968aa20.png)
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
![Machine learning [Matplotlib]](/img/d1/31ec2f96ca96465c6863798c7db179.jpg)
Machine learning [Matplotlib]

DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库

Smart pointer shared_ ptr、unique_ ptr、weak_ ptr
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

Quick sequencing and optimization

Learning and understanding of four special data types of redis

Realization of regular hexagon map with two-dimensional array of unity
随机推荐
Redis source code learning (33), command execution process
百融榕树数据分析拆解方法
Spark Learning Notes (VI) -- spark core core programming RDD action operator
深圳家具展首日,金可儿展位三大看点全解锁!
OC message mechanism
Quick sequencing and optimization
A new paradigm of distributed deep learning programming: Global tensor
数字孪生实际应用:智慧城市项目建设解决方案
Basic concept and essence of Architecture
Permutation and binary (Ji, DA) (day 84)
Food chain (day 79)
Kettle reads file split by line
Docker creates MySQL 8.x container and supports Mac and arm architecture chips
NLP hotspots from ACL 2022 onsite experience
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
[common search questions] 111
Briefly sort out the dualpivotquicksort
Banyan data model of Bairong
DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
榕树贷款,