上机笔记5.4

*5.4 PAT B1007

让我们定义d**n为:d**n=p**n+1−p**n,其中p**i是第i个素数。显然有d1=1,且对于n>1有d**n是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N(<105),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4
#include<stdio.h>
#include<math.h>

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

int main() {
	int n, count = 0;
	scanf("%d", &n);
	for (int i = 3; i + 2 <= n; i++) {
		if (isPrime(i) == true && isPrime(i + 2)) {
			count++;
		}
	}
	printf("%d",count);
	return 0;
}

*5.4 PAT B1013

P**i 表示第 i 个素数。现任给两个正整数 MN≤104,请输出 P**MP**N 的所有素数。

输入格式:

输入在一行中给出 MN,其间以空格分隔。

输出格式:

输出从 P**MP**N 的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27

输出样例:

11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103
//暴力
#include<stdio.h>
#include<math.h>

const int maxn = 1000001;

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

int prime[maxn], num = 0;
bool p[maxn] = { 0 };

void FindPrime(int n) {
	for (int i = 1; i < maxn; i++) {
		if (isPrime(i) == true) {
			prime[num++] = i;
			p[i] = true;
			if (num >= n)
				break;
		}
	}
}

int main() {
	int m, n, count = 0;
	scanf("%d%d", &m, &n);
	FindPrime(n);
	for (int i = m; i <= n; i++) {
		printf("%d", prime[i - 1]);
		count++;
		if (count % 10 != 0 && i < n)
		{
			printf(" ");
		}
		else {
			printf("\n");
		}
	}
	return 0;
}
//筛法
#include<stdio.h>
const int maxn = 1000001;
int prime[maxn], num = 0;
bool p[maxn] = { 0 };

void FindPrime(int n) {
	for (int i = 2; i < maxn; i++) {
		if (p[i] == false) {
			prime[num++] = i;
			if (num >= n)
				break;
			for (int j = i + i; j < maxn; j += i) {
				p[j] = true;
			}
		}
	}
}

int main() {
	int m, n, count = 0;
	scanf("%d%d", &m, &n);
	FindPrime(n);
	for (int i = m; i <= n; i++) {
		printf("%d", prime[i - 1]);
		count++;
		if (count % 10 != 0 && i < n)
		{
			printf(" ");
		}
		else {
			printf("\n");
		}
	}
	return 0;
}

*5.4 PAT A1015

A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<105) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No
#include<stdio.h>
#include<math.h>

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

int d[111];

int main() {
	int n, radix;
	while (scanf("%d", &n) != EOF) {
		if (n < 0)
			break;
		scanf("%d", &radix);
		if (isPrime(n) == false) {
			printf("No\n");
		}
		else {
			int len = 0;
			do {
				d[len++] = n % radix;
				n /= radix;
			} while (n != 0);
			for (int i = 0; i < len; i++) {
				n = n * radix + d[i];
			}
			if (isPrime(n) == true)
				printf("Yes\n");
			else {
				printf("No\n");
			}
		}
	}
	return 0;
}

*5.4 PAT A1078

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(k**ey)=k**ey%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤104) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -
#include<stdio.h>
#include<math.h>
#include<vector>

using namespace std;

const int N = 11111;

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

bool hashTable[N] = { 0 };

int main() {

	int n, TSize, a;

	scanf("%d%d", &TSize, &n);
	while (isPrime(TSize) == false) {
		TSize++;
	}

	for (int i = 0; i < n; i++) {
		scanf("%d", &a);
		int M = a % TSize;
		if (hashTable[M] == false) {
			hashTable[M] = true;
			if (i == 0) {
				printf("%d", M);
			}
			else {
				printf(" %d", M);
			}
		}
		else {
			int step;
			for (step = 1; step < TSize; step++) {
				M = (a + step * step) % TSize;
				if (hashTable[M] == false) {
					hashTable[M] = true;
					if (i == 0) {
						printf("%d", M);
					}
					else {
						printf(" %d", M);
						break;
					}
				}
			}
			if (step >= TSize) {
				if (i > 0) {
					printf(" ");
				}
				printf("-");
			}
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值