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

Solution:
  这道题令我惊讶的是,竟然是简单的判断一下是不是素数?!
  本以为这么大的数字级别,应该是建立素数表来判断的,想不到竟然时一个个数字进行简单的判断是不是素数?
  倒是建立素数表内存超了,判断是不是素数竟然没有超时?!
  下面代码给出了建立素数表
  
 #include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int n, k;
string str, res = "";
//void getPrimeTable(int inf, vector<bool>&notPrime)//创建素数表
//{
// notPrime[0] = notPrime[1] = true;
// for (int i = 2; i <= inf; ++i)
// if (notPrime[i] == false)//从2这个素数开始
// for (int j = 2; j*i <= inf; ++j)
// notPrime[j*i] = true;//剔除素数的所有倍数
//} bool isPrime(int x)//判断是不是素数
{
if (x < )
return true;
for (int i = ; i*i <= x; ++i)
if (x%i == )
return false;
return true;
}
int main()
{
cin >> n >> k;
cin >> str;
//int size = (int)pow(10, k);
//vector<bool>notPrime(size+1, false);//防止内存太大,我这里是动态建立数组的
//getPrimeTable(size, notPrime);//创建素数表
for (int i = ; i + k <= n; ++i)
{ string s = str.substr(i, k);
int num = atoi(s.c_str());
if (isPrime(num))//notPrime[num]==false)//使用的代码简单的素数判断,注释的代码是使用素数表
{
res = s;
break;
}
}
if (res.size() > )
cout<<res;
else
cout << "";
return ;
}

PAT甲级——A1152 GoogleRecruitment【20】的更多相关文章

  1. PAT 甲级 1035 Password (20 分)(简单题)

    1035 Password (20 分)   To prepare for PAT, the judge sometimes has to generate random passwords for ...

  2. PAT甲级——1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  3. PAT 甲级 1008 Elevator (20)(代码)

    1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...

  4. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  5. PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)

    1061 Dating (20 分)   Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...

  6. PAT 甲级 1008 Elevator (20)(20 分)模拟水题

    题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...

  7. PAT甲级——1061 Dating (20分)

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  8. PAT甲级——1005.SpellItRight(20分)

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  9. PAT甲级——1077.Kuchiguse(20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

随机推荐

  1. PHP DOMDocument操作 XML类 属性、方法

    属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definition 以DTD或XML模式给出的节 ...

  2. python多种推导式的实现

  3. Java + selenium 元素定位(5)之By Xpath

    这篇关于Xpath方法的文章和之前那篇CSS的方法一样,使用前,需要先掌握一些Xpath的相关知识.当然,网上也有各种工具可以帮助我们获取到元素的Xpath,但是这并不代表着我们就可以不用了解Xpat ...

  4. Cocos2d-x之物理引擎

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在很多的游戏设计中一般都会涉及和模拟到真实的物理世界.然而游戏中模拟真实世界的物理会很复杂.使用已经写好的物理引擎会用很大的帮助和便利.  ...

  5. create Excel file - snippet

    http://www.codepal.co.uk/show/Line_breaks_lost_and_br_tags_show_when_exporting_to_Excel_file  Protec ...

  6. unity3D中的Input按键方法检测

    一,按键的按下抬起等识别方法 void Update () { ; ; if (Input.GetKeyDown (KeyCode.A)) { Debug.Log("A按下一次") ...

  7. linux内核的简单介绍

    linux的版本号分为两个部分:内核(Kernel)和发行套件(distribution)版本. 内核版本是linus领导下的开发小组开发出的系统内核的版本号,而发行套件是由其他组织或者厂家将linu ...

  8. vue,数组循环中src路径下图片总是不展示

    每次数组中带图片的src,图片总是不展示,搞得都怀疑是不是姿势不对.经过各方查看,还真不对 原因是因为图片路径在assets,所以需要require一下.其实就是require('path') 如a[ ...

  9. 如何将excel表格中的纯数字删掉 空白行,然后删除

    excel里如何删除一列中全部数字 1.选择目标区域2.按“F5”,定位条件如下设置,即可选中值为数字的单元格,在按Del. 空白行删除 选择空值,确定,就会将所有空白行选定,然后删除

  10. 项目使用gulp的配置编译sass笔记

    Node环境 通过 node.js 网站下载了安装包进行安装 node.js, npm也会一起安装 node --version # 查看node.js版本 npm --version #查看npm版 ...