1059. Prime Factors (25)-PAT

本文介绍了一种用于分解任意正整数为质因子的方法,并提供了相应的代码实现。

1059. Prime Factors (25)

时间限制
50 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
HE, Qinming

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
推荐指数:※
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1059
这道题目主要是分解质因子。
1.考虑1的情况。
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
bool is_prime(long n){
	long i,tmp;
	tmp=sqrt(n)+1;
	for(i=3;i<=tmp;i++){
		if(n%i==0)
			return false;
	}
	return true;
}
int main()
{
	long n,i,tmp,k;
	cin>>n;
	cout<<n<<"=";
	if(n==1){
		cout<<1;
		return 0;
	}
	tmp=n;
	for(i=2;i<=tmp;i++){
		k=0;
		if(is_prime(i)==true){
			while(n%i==0){
				k++;
				n=n/i;
			}
		}
		if(k>1){
			cout<<i<<"^"<<k;
		}
		else if(k==1){
			cout<<i;
		}
		if(n!=1&k>=1)
			cout<<"*";
		else if(n==1)
			break;
	}
	return 0;
}


<think>根据用户提供的make执行结果,我们可以看到编译过程正常完成,没有错误信息。具体分析如下: ### 执行结果说明: 1. **编译main.c**: ``` gcc -Wall -Wextra -pedantic -std=c99 -c main.c ``` 成功生成`main.o`目标文件,无警告或错误。 2. **编译prime.c**: ``` gcc -Wall -Wextra -pedantic -std=c99 -c prime.c ``` 成功生成`prime.o`目标文件,无警告或错误。 3. **链接生成可执行文件**: ``` gcc -Wall -Wextra -pedantic -std=c99 -o primeCounter main.o prime.o -lm ``` 成功生成可执行文件`primeCounter`,链接了数学库`-lm`。 ### 结论: - **编译成功**:所有步骤均无报错,表明: - Makefile编写正确(包括依赖关系和编译规则) - 源代码无语法错误 - 文件路径和命名正确 - **可执行文件生成**:当前目录下已生成`primeCounter`,可直接运行: ```bash ./primeCounter ``` ### 后续建议: 1. 运行程序验证功能: ```bash ./primeCounter ``` 2. 若需清理中间文件,执行: ```bash make clean ``` §§相关问题§§ 1. 如何为Makefile添加安装(install)和卸载(uninstall)规则? 2. 在Makefile中如何动态处理新增的源文件? 3. 如何让Makefile支持跨平台编译(如Windows和Linux)? 4. 如何优化Makefile以减少重复编译时间?</think>根据您提供的 `make` 执行结果,这表明编译过程已**成功完成**。以下是详细分析: ### 执行结果解读: ``` gcc -Wall -Wextra -pedantic -std=c99 -c main.c # 编译 main.c → main.o gcc -Wall -Wextra -pedantic -std=c99 -c prime.c # 编译 prime.c → prime.o gcc -Wall -Wextra -pedantic -std=c99 -o primeCounter main.o prime.o -lm # 链接生成可执行文件 ``` ### 关键说明: 1. **无错误信息**:输出中没有 `error` 或 `warning`(除了可能的正常警告) 2. **完整编译流程**: - 源文件编译:`.c` → `.o` - 目标文件链接:`.o` → 可执行文件 `primeCounter` 3. **成功标志**: - 生成了最终可执行文件 `primeCounter` - 所有步骤均正常退出(无 `Error 1` 等提示) ### 下一步操作: 1. **运行程序**: ```bash ./primeCounter ``` 2. **验证功能**: - 测试素数计算功能是否正常 - 检查输入输出是否符合预期 ### 成功编译的证明: - 当前目录会包含以下文件: ``` main.o # main.c 编译结果 prime.o # prime.c 编译结果 primeCounter # 最终可执行程序 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值