当前位置:网站首页>素因子分解--C(gcc)--PTA
素因子分解--C(gcc)--PTA
2022-07-27 00:03:00 【零维展开智子】
给定某个正整数 N,求其素因子分解结果,即给出其因式分解表达式 N=p1k1⋅p2k2⋯pmkm。
【输入格式】
输入long int范围内的正整数 N
【输出格式】
按给定格式输出N的素因式分解表达式,即 N=p1^k1*p2^k2*…*pm^km,其中pi为素因子并要求由小到大输出,指数ki为pi的个数;当ki为1即因子pi只有一个时不输出ki。
【输入样例】
1323
【输出样例】
1323=3^3*7^2
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
【思路模拟】
第一步: 素因子从小到大的话,我们可以设置一个初始值n=2,每次判断n是否为素数,如果不是就n++,是的话再判断是否为N的因子。
第二步:将n的值赋给i,设指数初始为k=0,因为n只有外部大循环才能改变,i符合N的因子条后,令N/=i,当N=0时即外部循环终止,代表表达式终止,判断i是否第一次出现,第一次出现的话将i赋值给新变量i1,这样做的目的当i1=i时,就给k++代表后一个因子与前一个因子相同,指数++,i1不等于i时,代表出现了新的因子,输出当前表达式,此时令k=1,代表新因子的初始值。
#include<stdio.h>
#include<math.h>
int main(){
long int N,n=2,i=0,i1=0,j=2;//i为素因子,,i1为系数,k为指数
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;
}边栏推荐
- Which securities firm is safer to open an account and buy REITs funds?
- 哪家券商开户买REITs基金比较安全?
- [redis] quick start
- Towhee 每周模型
- Cookie addition, deletion, modification and query methods
- The XML format of labelimg annotation is converted to yolov5
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- C语言程序的编译上
- LeetCode->二分查找打卡
- 【力扣】1859.将句子排序
猜你喜欢

Plato farm is expected to further expand its ecosystem through elephant swap

GoatGui邀你参加机器学习研讨班

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

「软件测试」包装简历从这几点出发,直接提升通过率
![[redis] quick start](/img/42/09f08b4f78bc2ddd4d15693d62cb4b.png)
[redis] quick start

数据库读写分离和分库分表

用swiper分类图标

Dynamically set the height of applet swiper

想要彻底搞的性能优化,得先从底层逻辑开始了解~

Arduinouno drive RGB module full color effect example
随机推荐
Rust Web(一)—— 自建TCP Server
无效的目标发行版:17 的解决办法
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
Summary of dataset operations in ppocrlabel format.
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
Leetcode skimming -- no.238 -- product of arrays other than itself
I wish you a happy Chinese Valentine's day and invite you to read the source code together
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
Invalid target distribution: solution for 17
Go language slow start - package
MySQL 5.7 takes the first item of the group
Time module: acquisition and mutual transformation of timestamp, structured time and formatted time
转:俞敏洪:阻碍你成长的,是你自己
Function stack frame explanation
文章摘要智能提取【基于BERT技术】
idea中常用的快捷键
Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
小玩一个并行多线程MCU—MC3172
解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json