质因子分解(PAT A1096、PAT A1059)

所谓质因子分解是指将一个正整数n写成一个或多个质数的乘积形式。例如 6 = 2 x 3 , 8 = 2 x 2 x 3。

1、定义结构体
struct factor{
	int x,cnt; // x为质因子,cnt为其数量
}fac[10];

模版

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++;
}

如果在上面步骤结束后n仍然大于1,说明n有且仅有一个大于sqrt(n)的质因子,这时需要把这个质因子加入fac数组,并令其个数为1

if (n != 1){
	fac[num].x = n;
	fac[num++].cnt = 1;
}

例题1

PAT A1096 consecutive Factors

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<231).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]factor[2]…*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
567

思路:
这题其实还挺好写的,如果能想到写法的话,不需要用质因子的模版,因为它是连续的,而且连乘的话,它的范围就不会超过其开根号,即2-根号n。遍历i = 2 ;i <= sqrt(n); i++;
再在里面来一个while(1)循环,当循环体 n % j != 0时 break;
记录最长ansLen = j - i + 1;
代码

#include<cstdio>
#include<cmath>
typedef long long LL;
int main(){
	int n;
	scanf("%d",&n);
	LL sqr = (LL)sqrt(1.0*n),ansI = 0,ansLen = 0;
	for(int i = 2; i <=sqr;i++){
		LL temp = 1,j = i;
		while(1){
			temp *= j;
			if (n % temp != 0)break;
			if (j - i + 1 > ansLen){
				ansI = i;
				ansLen = j - i + 1;
			}
			j++;
		}
	}

	if (ansLen == 0){
		printf("1\n%lld",n);
	}else{
		printf("%lld\n",ansLen );
		for(int i = ansI; i < ansI + ansLen;i++){
			printf("%lld",i );
			if (i != ansLen + ansI -1)printf("*");
		}
	}
	return 0;
}

例题2

PAT A1059. Prime Factors(20)

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

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~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 p​I, kIis 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

思路:
直接套模版,需要注意的是for循环的判断条件是 i < pNUm && prime[i] <= sqr;
代码:

#include<cstdio>
#include<queue>
using namespace std;

struct mice
{
	int R;
	int weight;
}mouse[1010];
queue<int> q;

int main(){
	int np,ng,order;
	scanf("%d %d",&np,&ng);
	for (int i = 0; i < np; ++i)
	{
		scanf("%d",&mouse[i].weight);
	}
	for (int i = 0; i < np; ++i)
	{
		scanf("%d",&order);
		q.push(order);
	}
	int temp = np,group;
	while(q.size()!=1){
		if (temp%ng == 0)group = temp/ng;
		else group = temp/ng + 1;
		for(int i = 0; i < group;i++){
			int k = q.front();
			for(int j = 0; j < ng;j++){
				if (i*ng + j >= temp) break;
				int front = q.front();
				if (mouse[front].weight > mouse[k].weight){
					k = front;
				}
				mouse[front].R = group + 1;
				q.pop();
			}
			q.push(k);
		}
		temp = group;
	}
	mouse[q.front()].R = 1;
	for(int i = 0; i < np;i++){
		printf("%d\n",mouse[i].R );
		if (i < np -1) printf(" ");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值