pat a1059 Prime Factors (25分) 【质因子分解】

7-138 (C语言PTA)质因子分解 (10分) 7-138 质因子分解 (10分) 本题目要求读入一个大于1的整数,编程将其分解成若干个质因子(素数因子)积的形式。 输入格式: 大于1的整数一个。 输出格式: 将输入的正整数分解成若干个质因子积的形式,质因子的出现顺序按从小到大排列。如:30=222*5;如果整数本身为质数或素数,直接输出,如:13=13。 输入样例: 12480 输出样例: 12480=2222223513 #include<stdio.h> int main(){ int i=2,k; scanf("%d",& 阅读详情

题目:

https://www.nowcoder.com/pat/5/problem/4112

https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​*…*p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291
#include<bits/stdc++.h>
using namespace std;

struct factor
{
	int x;
	int cnt;
} fac[10];//10个质数相乘已经超出了int表示的范围

int main()
{
	int n;
	cin>>n;
	int m=n;  //用m存一下!!以便输出。 
	
	//n=1,特判。 
	if(n==1)
	{
		cout<<"1=1";
		return 0;  //别忘记及时return,终止程序!! 
	} 
	
	int num=0; //记录n的质因子的个数 
	
	//1、求出质因子: 	
	int sqr=(int)sqrt(1.0*n);
	for(int i=2;i<=sqr;i++)
	{
		if(n%i==0)
		{
			fac[num].x=i;
			fac[num].cnt=0;
			while(n%i==0)
			{
				fac[num].cnt++; 
				n/=i;
			}
			num++;
		}
		if(n==1) break;
	}
	if(n!=1)  //存在大于sqrt(n)的质因子
	{
		fac[num].x=n;
		fac[num].cnt=1;
		num++;
	}
	
	//2、输出:
	cout<<m<<'=';
	for(int i=0;i<num;i++)
	{
		//未输出到最后一个质因子 
		if(i<num-1)
		{
			if(fac[i].cnt==1)
			{
				cout<<fac[i].x<<'*';
			}
			else
			{
				cout<<fac[i].x<<'^'<<fac[i].cnt<<'*';
			}
		}
		//处理最后一个 
		else
		{
			if(fac[i].cnt==1)
			{
				cout<<fac[i].x;
			}
			else
			{
				cout<<fac[i].x<<'^'<<fac[i].cnt;
			}
		}		
	}
	return 0;
}

 

#include<iostream>
#include<cstdio>
#include<cmath> 
using namespace std;

const int maxn=100010;

bool is_prime(int n)
{
	if(n==1) return false;
	else 
	{
		int sqr=(int)sqrt(1.0*n);
		for(int i=2;i<=sqr;i++)
		{
			if(n%i==0) return false;
		}
		return true;
	}
} 

int prime[maxn];
int pnum=0;

/*
因为大部分数字的因子分解,都是可以通过某几个质数的重现得到,即一个质数可能在因子分解式中出现多次;
所以,无需一直求到n的质数,前几次测试出现的段错误,都是因为开的质数表放不下了。
*/ 
/*
//错误代码: 
void find_prime(int n)
{
	for(int i=2;i<n;i++)
	{
		if(is_prime(i)==true) prime[pnum++]=i;
	}	
}
*/

void find_prime()
{
	for(int i=2;i<maxn;i++)
	{
		if(is_prime(i)==true) prime[pnum++]=i;
	}	
}

struct factor
{
	int x;
	int cnt;
}fac[10];


int main()
{	
	find_prime();
	int n;
	int num=0;  //不同质因子的个数 
	cin>>n;
	
	//find_prime(n);
	
	if(n==1) cout<<"1=1";
	else
	{
		cout<<n<<"=";
		int sqr=(int)sqrt(1.0*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) 
		{
			fac[num].x=n;
			fac[num].cnt=1;
			num++;
		}
		
		//按格式输出
		for(int i=0;i<num;i++)
		{
			//if(i>0) cout<<'*';
			cout<<fac[i].x;
			if(fac[i].cnt>1) cout<<'^'<<fac[i].cnt;
			if(i>=0&&i<num-1) cout<<'*';			
		} 
		
	} 
		
	return 0;
}

 

【PAT 甲】1059 Prime Factors 【PAT 甲】1059 Prime Factors 质因子分解 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 阅读详情

相关推荐

Apifox CLI + Claude Skills:将接口自动化测试融入ai

基于 Apifox CLI、Claude Code以及Claude Skills组合,实现自然语言触发接口自动化测试。

wwz_0012的博客 599

PTA-分解素因子

记录

weixin_73617818的博客 2331

使用再生龙还原镜像文件到硬盘

1. 把制作好的再生龙光盘插入需要的电脑,开机按F4使用再生龙启动2. 选择默认的:Clonezailla live(Default settings,VGA 800*600),然后等待几分钟 3. 选择语言:简体中文 4. 选择不修改键盘映射 5. 选择使用再生龙          6. 选择硬盘/分区(存到/来自)镜像文件,此选项既可以制作镜像,也可以恢复镜像。 7. 使用本机的分区     ...

weixin_42168893的博客 1万+

PAT(甲级)1059 Prime Factors (25 分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​. Input Specification: Each input file co...

Eggy个蛋 403

安卓逆向实战:利用Frida-RPC自动化获取大麦演唱会数据

本文详细介绍了如何利用Frida-RPC技术实现安卓逆向自动化,以获取大麦演唱会数据。通过实战演示,从环境搭建、使用Frida动态分析定位核心签名算法,到封装为可复用的Python脚本,完整解析了自动化数据获取的技术路径与工程化思考。

cheese的博客 978

手把手教你用STM32的SPI驱动MCP2517FD实现CAN FD通信(附完整代码)

本文详细介绍了如何使用STM32通过SPI驱动MCP2517FD实现CAN FD通信,涵盖硬件连接、SPI驱动编写、寄存器配置及数据收发实战。提供完整代码示例和常见问题排查指南,帮助开发者快速构建高可靠性的CAN FD通信系统,适用于汽车电子和工业控制等领域。

weixin_30607029的博客 676

PAT A1059 Prime Factors (25 分) 素数

题目大意:输出正数N的质因数分解,如果质因数的指数大于1,要按照底数^幂的形式输出。 因为要输出幂,在找质因数的同时要记录该质因数出现的次数,可以用map<int,int>,每次找到一个质因数K, 令 N /= K,开始下一次循环,直到 N <= 1为止。 坑点在于 N = 1的情况需要特判输出 1=1(测试点3)。 AC代码: #include...

HackingGate的博客 496

33、密码学中的选择密文攻击与Cramer - Shoup加密方案

本文详细介绍了密码学中选择密文攻击的背景及其对Cramer-Shoup公钥加密方案的影响。该方案在决策Diffie-Hellman假设和抗碰撞哈希函数的基础上,能够有效抵御自适应选择密文攻击,并且其安全性无需依赖随机预言机模型。文章还分析了Cramer-Shoup方案的设计原理、加密与解密过程,以及其在标准假设下的安全性证明方法。此外,通过一系列相关练习,帮助读者进一步理解密码学概念如密文不可区分性、ElGamal加密、Goldwasser-Micali方案等。最后,文章展望了密码学的未来发展趋势,包括量子

vscode6remote的博客 100

1059 Prime Factors (25分)

题目 思路 1.首先打表:用筛法将sqrt(n)之前的所有素数打到表p[pnum++]中; 2.将组成n的所有素数及相应素数个数存入结构体factor[]中;并在存入的过程中将n逐渐分解为更小的数; 3.当素数表到达p[pnum]时,n被分解剩余的数为最后的素数; 出现错误及纠错过程 1.没想到素数表只需打到sqrt(n);导致素数表太大超出限制出现段错误; 2.在find_factor中分解n的...

qq_43196404的博客 203

RK3568平台(KBuild篇)KBuild编译体系

从Linux内核2.6开始,Linux内核的编译采用Kbuild系统,这和过去的编译系统有很大的不同,尤其对于Linux内核模块的编译。在新的系统下,Linux编译系统会两次扫描Linux的Makefile:首先编译系统会读取Linux内核顶层的Makefile,然后根据读到的内容第二次读取Kbuild的Makefile来编译Linux内核。kbuild,即 kernel build,用于编译 Linux 内核文件。

weixin_49303682的博客 349

【C2000系列DSP的Bootloader详解】如何利用脚本自动合并Bin文件

本文通过OutconvertBin.py实现了 C2000 Bootloader+UserApp 的「ELF 转 BIN+BIN 合并」全流程自动化,核心优势如下: 架构适配:严格遵循 C28x 16 位大端格式,生成的 BIN 文件可直接烧录; 自动化程度高:一键完成清理、转换、合并,无需手动执行多个命令; 可追溯性:生成 CRC32 校验值,便于验证文件完整性; 量产友好:合并后的merged.bin支持一次性烧录,提升量产效率。

昔时扬尘处 184

5.5 PAT A 1059 Prime Factors (25分)(质因子分解)

1059 Prime Factors (25分) Given any positive integer NNN, you are supposed to find all of its prime factors, and write them in the format NNN = p1k1×p2k2×⋯×pmkm{p_1}^{k_1}\times {p_2}^{k_2} \times \cd...

小魚兒 195

今日指数项目之实时流数据采集【六】

实时数据采集代码

Maynor的博客 2147

【PAT】1059 Prime Factors (25分)(质因子分解)

1059 Prime Factors (25分) 题目链接 典型的质因子分解,代码如下: #include<iostream> #include<cmath> using namespace std; #define maxn 10000005 int prime[maxn], pnum=0; bool p[maxn]; struct factor{ int x;...

醉酒诗人的博客 233

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

1059 Prime Factors

/* Sologala @github https://github.com/Sologala/PAT_OJ PAT_oj No.1059_Prime_Factors */ 1059 Prime Factors (25 分) 思路 ​ 把一个数字分解成质数的指数和形式。最开始先计算出 从2到num 的所有质数,然后从2开始拆解num 。但是第一个测试点会超时。 ​ 期间还发现...

sologala的博客 299

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【PAT甲级 - C++题解】1059 Prime Factors

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 654

PAT甲级 1059 Prime Factors (25 分)

\quad首先建立一个素数表,这个素数表包含了[0, 100000]的数是否是素数的判定。接下来我们将n进行分解,方法如下:依次将n除以1到100000的素数i,若n能整除i,则i是一个质数因子,并且统计i出现次数。这里我用res[100000]这个数组进行统计,若出现了i这个质数因子,则res[i]++。这样,通过遍历res数组我们就能获得所有的质因子及其出现次数(指数)。核心程序如下: whi...

qq_40438165的博客 1773

【PAT】A1059 Prime Factors【素因子分解】【欧拉筛法】

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​^k​m​​​​. Input Specification: Each input file con...

elegant coder 765

【PAT甲级A1059】Prime Factors (25分)(c++)

1059 Prime Factors (25分) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1^​​ ​k​1​​ ​​ × p​2 ^​​ ​k​2​ ​​ ×⋯× p​m​^​ ​km​​ . Input Specification: Each input file contains one test case wh

小庄同学的博客 246

[PAT-A 1059]Prime Factors

题目大意: 给出一个int范围的整数,按照从小到大顺序输出其分解为质因式的乘法算式。 思路: 参考[算法笔记]数学问题 质因式分解 先输出素数表,在进行质因式的分解的操作。 注意findPrime()函数中循环不能是i<=maxn,是i=maxn AC代码: //PAT_A 1059 #include<cstdio> #include<cmath> const in...

RegulusF的博客 203

PAT甲级 1059 Prime Factors (25分)

1059 Prime Factors (25分) 题目链接:PAT A 1059 题目大意:给出一个数n,范围在long int之内,要求输出它的质因子分解形式。 思路分析:对于任意一个数字n,它的质因子要么全部小于等于根号n,要么只有一个质因子大于根号n而其余质因子全部小于根号n,可以以此为思路进行求解。先求出100010以内的素数并把它们存放到数组中(int类型开根号大概是2的16次方,这个...

weixin_43684054的博客 246

PAT A1059 Prime Factors (25分)

前言 传送门 正文 参考题解 #include<iostream> #include<cmath> using namespace std; /* 题意: 给定一个数n,将其分解成为质因子的乘法形式(从小到大) 思路:直接套用质因子分解模板即可。注意首先打印出素数表,然后再 进行质因子分解 注意点: 1、素数的判断时循环条件是小于等于根号n 2、打印素数表时,从1开始循环,并且循环条件是小于maxn 3、注意特判1 4、注意枚举质因子时如何求每个质因子的个数,以及n是变化

逝不等琴生的博客 373

PAT:A1059 Prime Factors (25 分)

PAT:A1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​. Input Sp...

Ecloss 266

【质因子分解+打印素数表】PAT A1059 Prime Factors

文章目录疑问代码反思 疑问 暂无 代码 //直接设定了N的大小是int型 #include "bits/stdc++.h" using namespace std; const int maxn = 1e6; //用于存放质数的数组 int prime[maxn],pNum=0; bool p[maxn] = {0}; struct factor{ int x; int cn...

yc_cy1999的博客 368

PAT-ADVANCED1059——Prime Factors

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 题目描述: 题目翻译: 1059 质因子 任给一个正整数N,你需要找出它的所有质因子,以下述形式输出...

清风阁 341

[PAT A1059]Prime Factors

[PAT A1059]Prime Factors 题目描述 输入格式 Each input file contains one test case which gives a positive integer N in the range of long int. 输出格式 输入样例 97532468 输出样例 97532468=2^211171011291 解析 这道题目属于数学题,最主要就...

欢迎来到我的编程小屋 589
上一篇: pat b1013 数素数 (20分)
下一篇: 大整数
Estrellas_
博客等级 码龄9年 273粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值