【PAT甲级 - C++题解】1152 Google Recruitment

简介: 【PAT甲级 - C++题解】1152 Google Recruitment

1152 Google Recruitment

In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL consisting of the first 10-digit prime found in consecutive digits of the natural constant e. The person who could find this prime number could go to the next step in Google’s hiring process by visiting this website.

The natural constant e is a well known transcendental number(超越数). The first several digits are: e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921… where the 10 digits in bold are the answer to Google’s question.


Now you are asked to solve a more general problem: find the first K-digit prime in consecutive digits of any given L-digit number.


Input Specification:

Each input file contains one test case. Each case first gives in a line two positive integers: L (≤ 1,000) and K (< 10), which are the numbers of digits of the given number and the prime to be found, respectively. Then the L-digit number N is given in the next line.

Output Specification:

For each test case, print in a line the first K-digit prime in consecutive digits of N. If such a number does not exist, output 404 instead. Note: the leading zeroes must also be counted as part of the K digits. For example, to find the 4-digit prime in 200236, 0023 is a solution. However the first digit 2 must not be treated as a solution 0002 since the leading zeroes are not in the original number.


Sample Input 1:

20 5
23654987725541023819

Sample Output 1:

49877

Sample Input 2:

10 3
2468024680

Sample Output 2:

404


题意

从任一给定的长度为 L 的数字中,找出最早出现的 K 位连续数字所组成的素数。

思路

由于题目的时间限制为 0.2 秒,如果直接用试除法每次都从前往后除以每个自然数,从而判断是否为质数可能会超时。所以我们可以将所有的质数先筛出来,然后再以长度为 k 的滑动窗口从前往后遍历字符串,用前面筛选出来的质数去判断当前窗口内的数字是否为质数。


代码

#include<bits/stdc++.h>
using namespace std;
const int M = 40000;
bool st[M];
int primes[M], cnt;
//先把10的9次方内的所有质数筛出来
void init()
{
    for (int i = 2; i < M; i++)
        if (!st[i])
        {
            primes[cnt++] = i;    //记录该质数
            //将i的倍数的所有数筛去
            for (int j = i * 2; j < M; j += i)
                st[j] = true;
        }
}
//用筛出来的所有质数去判断x是否为质数
bool check(int x)
{
    for (int i = 0; primes[i] <= x / primes[i]; i++)
        if (x % primes[i] == 0)
            return false;
    return true;
}
int main()
{
    init();
    int len, k;
    string str;
    cin >> len >> k >> str;
    //遍历字符串,判断是否能找到质数
    for (int i = 0; i <= len - k; i++)
    {
        //将字符串转换成整数
        int x = stoi(str.substr(i, k));
        //判断是否为质数
        if (check(x))
        {
            cout << str.substr(i, k);
            return 0;
        }
    }
    //如果没有找到质数则输出404
    puts("404");
    return 0;
}


目录
相关文章
|
2月前
|
JavaScript 前端开发 测试技术
一个google Test文件C++语言案例
这篇文章我们来介绍一下真正的C++语言如何用GTest来实现单元测试。
20 0
|
6月前
|
安全 编译器 C语言
MISRA C++ 、Google C++ 、AUTOSAR Adaptive Platform编码 C++ 规范总结
MISRA C++ 、Google C++ 、AUTOSAR Adaptive Platform编码 C++ 规范总结
198 1
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
77 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
79 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
80 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
135 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
58 0