PAT练习笔记——5.4 素数

2019年9月PAT - 练习笔记——5.4

以下页码标注的是阅读器中实际页码,而不是书本身自印的页码。

第5章 入门篇(3)——数学问题

5.4 素数

目录

  1. B1007 素数对猜想
  2. B1013 数素数
  3. A1015 Reversible Primes
  4. A1078 Hashing

  1. B1007 素数对猜想

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

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

    输入格式:

    输入在一行给出正整数N

    输出格式:

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

    输入样例:
    20
    
    输出样例:
    4
    
    1. 我的

      #include <iostream>
      #include <cmath>
      
      using namespace std;
      
      bool IsPrime(int n)
      {
      	// 这里n一定大于等于5 
      	int sqrtn = sqrt(n);
      	for (int i = 2;i <= sqrt(n);++i) {
      		if (0 == n % i) return false;
      	}
      	return true;
      }
      
      int main(void)
      {
      	int n = 0;
      	cin >> n;
      	
      	int pre = 3, count = 0;
      	for (int i = 5;i <= n;i += 2) {
      		if (IsPrime(i)) { 
      			if (i - pre == 2) ++count;
      			pre = i;
      		} 
      	}
      	cout << count;
      	
      	return 0;
      }
      
    2. 《算法笔记》P217


  2. B1013 数素数

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

    输入格式:

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

    输出格式:

    输出从 PMPN 的所有素数,每 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
    
    1. 我的

      #include <iostream>
      #include <cmath>
      
      using namespace std;
      
      bool IsPrime(int n)
      {
      	if (n == 1) return false;
      	else if (n == 2) return true;
      	else {
      		int sqrtn = sqrt(n);
      		for (int i = 2;i <= sqrt(n);++i) {
      			if (0 == n % i) return false;
      		}
      	}
      	return true;
      }
      
      int main(void)
      {
      	int m = 0, n = 0;
      	cin >> m >> n;
      	
      	int count = 1, count1 = 0;
      	if (m == 1) {
      		cout << 2;
      		++count1;
      		if (n > m) cout << " ";
      	}
      	for (int i = 3;count < n;i += 2) {
      		if (IsPrime(i)) {
      			++count;
      			if (count >= m) {
      				++count1;
      				cout << i;
      				if (count1 % 10 == 0) cout << endl;
      				else if (count != n) cout << " ";
      			}
      		}
      	}
      	
      	return 0;
      }
      
    2. 《算法笔记》P218


  3. A1015 Reversible Primes

    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
    
    1. 我的

      #include <iostream>
      #include <cmath>
      #include <algorithm>
      
      using namespace std;
      
      bool IsPrime(const int n)
      {
      	if (n == 1) return false;
      	else if (n == 2) return true;
      	else {
      		int sqrtn = sqrt(n);
      		for (int i = 2;i <= sqrtn;++i) {
      			if (0 == n % i) return false;
      		}
      	}
      	return true;
      }
      
      string Change(int n, const int d)
      {
      	string num = "";
      	for (;n;n /= d) 
      		num = to_string(n % d) + num;
      	return num;
      }
      
      int To10(int n, const int d)
      {
      	int num = 0;
      	for (int radix = 1;n;n /= 10, radix *= d) num += (n % 10) * radix;
      	return num;
      }
      
      int main(void)
      {
      	int n = 0;
      	for (cin >> n;n >= 0;cin >> n) {
      		int d = 0;
      		cin >> d;
      		
      		if (!IsPrime(n)) cout << "No" << endl;
      		else {
      			string str = Change(n, d);
      			reverse(str.begin(), str.end());
      			int rn = To10(atoi(str.c_str()), d);
      			if (IsPrime(rn)) cout << "Yes" << endl;
      			else cout << "No" << endl;
      		}	
      	}
      	
      	return 0;
      }
      

      多读几遍题目,准确理解题意

    2. 《算法笔记》P221


  4. A1078 Hashing

    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(key)=key%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 -
    
    1. 我的

      #include <iostream>
      #include <cmath>
      #include <map>
      
      using namespace std;
      
      bool IsPrime(const int n)
      {
      	if (n == 1) return false;
      	else if (n == 2) return true;
      	else {
      		int sqrtn = sqrt(n);
      		for (int i = 2;i <= sqrtn;++i) {
      			if (0 == n % i) return false;
      		}
      	}
      	return true;
      }
      
      int main(void)
      {
      	int msize = 0, n = 0;
      	cin >> msize >> n;
      	for (;!IsPrime(msize);++msize);
      	
      	map<int, bool> table;
      	for (int i = 0;i < n;++i) {
      		int key = 0;
      		cin >> key;
      		
      		if (i) cout << " ";
      		int pos = key % msize;
      		if (!table[pos]) {
      			table[pos] = true;
      			cout << pos;
      		}
      		else {
      			int inc = 1;
      			for (;inc <= msize / 2;++inc) {
      				int pos	= (key + inc * inc) % msize;
      				if (!table[pos]) {
      					table[pos] = true;
      					cout << pos;
      					break;
      				}
      			}
      			if (inc > msize / 2) cout << "-";
      		}
      	}
      	
      	return 0;
      }
      
    2. 《算法笔记》P223

      “证明如果step从0~TSize - 1进行枚举却仍然无法找到位置,那么对step大于等于TSize来说也不可能找到位置(即证明循环节为TSize)。

      这里只需要证明当step取TSize至2TSize - 1也无法找到位置即可:

      设0≤x<TSize,那么

      (a + (TSize + x) * (TSize + x)) % TSize

      = (a + TSize * TSize + 2 * TSize * x + x * x) % TSize

      = (a + x * x) % TSize + TSize * TSize % TSize + 2 * TSize * x % TSize

      = (a + x * x) % TSize

      由于所有循环节为TSize,如果step从0 ~ TSize - 1进行枚举却仍然无法找到位置,那么对step大于等于TSize来说也不可能找到位置”


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值