0
点赞
收藏
分享

微信扫一扫

上机笔记5.5

颜路在路上 2022-02-11 阅读 60

文章目录

*5.5 PAT A1096

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
5*6*7
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int main() {
	ll n;
	scanf("%lld", &n);
	ll sqr = sqrt(1.0 * n), ansI = 0, anslen = 0;
	for (ll 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 (ll i = 0; i < anslen; i++) {
			printf("%lld", ansI + i);
			if (i < anslen - 1) {
				printf("*");
			}
		}
	}
	return 0;
}

*5.5 PAT A1059

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1kp2k2×⋯×pmk**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 = p1^k1*p2^k2**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:


Sample Output:

97532468=2^2*11*17*101*1291
#include<stdio.h>
#include<math.h>
const int maxn = 100010;

bool is_prime(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;
}

int prime[maxn], pNum = 0;
void Find_Prime() {
	for (int i = 1; i < maxn; i++) {
		if (is_prime(i) == true) {
			prime[pNum++] = i;
		}
	}
}

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

int main() {
	Find_Prime();
	int n, num = 0;
	scanf("%d", &n);
	if (n == 1)
		printf("1=1");
	else {
		printf("%d=", 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;
		}
		for (int i = 0; i < num; i++) {
			if (i > 0)
				printf("*");
			printf("%d", fac[i].x);
			if (fac[i].cnt > 1) {
				printf("^%d", fac[i].cnt);
			}
		}
	}
	return 0;
}
举报

相关推荐

上机笔记4.6

上机笔记3.1

上机笔记3.5

上机笔记4.5

matlab学习笔记5.5

上机068

上机065

上机019

HTML上机

上机作业21

0 条评论