PAT:A1059 Prime Factors (25 分)

本文解析PAT A1059题目要求,即给定一个正整数N,找出其所有质因数并按递增顺序排列,采用特定格式输出。通过C++实现,使用质数筛法预先生成质数表,并逐步分解输入整数。

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 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<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

struct factor{
	int x, cut;		// x代表质因子, cut代表质因子的个数 
}f[11];

// 因为给出的是int范围内的整数
// 判断是否是素数
bool isPrimare(int n) {
	if(n <= 1) return false;
	int sqr = (int)sqrt(1.0 * n);
	for(int i = 2; i <= sqr; i++) {
		if(n % i == 0) return false;
	}
	return true;
} 

// 求出素数表
const int maxn = 100010;
int p[maxn] = {0}, pNum = 0;
void FindPrimare() {
	for(int i = 1; i < maxn; i++) {
		if(isPrimare(i) == true) {
			p[pNum++] = i;
		}
	}
} 

int main() {
	FindPrimare();
	int n, num = 0;
	scanf("%d", &n);
	if(n == 1) printf("1=1");
	else {
		printf("%d=", n); 
		int q = (int)sqrt(1.0 * n);
		for(int i = 0; i < pNum && p[i] <= q; i++) {
			if(n % p[i] == 0) {
				f[num].x = p[i];
				f[num].cut = 0;
				while(n % p[i] == 0) {
					f[num].cut++;
					n = n / p[i];
				}
				num++;
			}
			if(n == 1) break;
		}
		if(n != 1) {
			f[num].x = n;
			f[num].cut = 1;
			num++;
		}
		for(int i = 0; i < num; i++) {
			printf("%d", f[i].x);
			if(f[i].cut > 1) printf("^%d", f[i].cut);
			if(i < num-1) printf("*");
		}
	}
	
	return 0;
}

 

java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@545b5ed0 testClass = com.my.demo_crm.DemoTest, locations = [], classes = [com.my.demo_crm.DemoCrmApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@2ed2d9cb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5d0a1059, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@957e06, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@201a4587, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@54a7079e, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@70e9c95d, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@dfc9e65f], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
最新发布
09-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值