Source:

PAT A1152 Google Recruitment (20 分)

Description:

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

Keys:

  • 素数

Attention:

  • int < 1e9, long long <1e18
  • 注意主串长度<子串长度的情况
  • s.size()返回unsigned类型,l<k时,会死循环

Code:

 /*
Data: 2019-08-02 16:31:26
Problem: PAT_A1152#Google Recruitment
AC: 25:40 题目大意:
给定L位数字中找出K位的素数,前导零同样占位
*/
#include<cstdio>
#include<string>
#include<iostream>
using namespace std; bool IsPrime(string s)
{
long long n = atoll(s.c_str());
if(n== || n==)
return false;
for(int i=; i*i<=n; i++)
if(n%i == )
return false;
return true;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE string s;
int l,k;
scanf("%d%d", &l,&k);
cin >> s;
for(int i=; i<=int(s.size())-k; i++)
{
if(IsPrime(s.substr(i,k)))
{
cout << s.substr(i,k).c_str();
s.clear();
break;
}
}
if(s.size() || l<k)
printf(""); return ;
}

PAT_A1152#Google Recruitment的更多相关文章

  1. PAT 1152 Google Recruitment

    1152 Google Recruitment (20 分)   In July 2004, Google posted on a giant billboard along Highway 101 ...

  2. PAT甲级——1152.Google Recruitment (20分)

    1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...

  3. PAT-1152(Google Recruitment)字符串+素数

    Google Recruitment PAT-1152 本题最需要注意的是最后输出要以字符串形式输出,否则可能会出现前导0的情况. /** * @Author WaleGarrett * @Date ...

  4. PAT甲级:1152 Google Recruitment (20分)

    PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...

  5. pat甲级 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  6. 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  7. PAT Advanced 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  8. PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  9. 1152 Google Recruitment

    题干前半略. Input Specification: Each input file contains one test case. Each case first gives in a line ...

随机推荐

  1. Linux Container测试之block IO

      简介 Linux Container是OS级别的虚拟化方案,它相比于一般的虚拟机没有了硬件模拟以及指令模拟,相比传统虚拟机具有更低的开销,因此可以应用到私有云之中.LXC目前的版本支持对memor ...

  2. NTC 家电中的应用1

    NTC应用非常广,特别是家电产品中,基本上凡是涉及到温度控制的都会选择它.本人应用的的产品有打奶器.电烤箱,油炸锅,咖啡机,直发器,暖风机和电壁炉,尽管应用广.但都是千篇一律.当中最基本的要点是应用中 ...

  3. qml

    用qt非常久了.可是一直没有注意到一个叫做qml的东西.今天google了一下,总结一下我的理解. 从表面上看qml就是用css javascript那一套来做软件的GUI,和原来的C++的widge ...

  4. HDU2037 事件排序问题

    题目要求: Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!&q ...

  5. SQL 琐碎记录

    1. 查看mysql现在已提供什么存储引擎: SHOW ENGINES ; 2. 查看mysql当前默认的存储引擎: SHOW VARIABLES LIKE '%storage_engine%'; 3 ...

  6. poj 1741(树的点分治)

    Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  7. 协同过滤算法中皮尔逊相关系数的计算 C++

    template <class T1, class T2>double Pearson(std::vector<T1> &inst1, std::vector<T ...

  8. Bootstrap 只读输入框

    只读输入框 为输入框设置 readonly 属性可以禁止用户输入,并且输入框的样式也是禁用状态.   <input class="form-control" type=&qu ...

  9. php获得本机ipv4地址

    if (isset($_ENV["HOSTNAME"])) $MachineName = $_ENV["HOSTNAME"]; else if (isset($ ...

  10. CVTE面经

    神一般的面试经历.也算面了不少公司,没见过这种面试. 一面:三个同学对应一个面试官,同一个问题依次作答. 1.为什么投递这个岗位? 答:blablabla... 2.最难忘的成功项目? 答:blabl ...