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

判断素数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1000
#define DMAX 10000
using namespace std;
typedef long long ll;
int l,k;
char s[MAX + ];
bool ispri(int x) {
if(x <= ) return false;
if(x == || x == ) return true;
if(x % != && x % != ) return false;
for(int i = ;i * i <= x;i += ) {
if(x % i == || x % (i + ) == ) return false;
}
return true;
}
int main() {
scanf("%d%d",&l,&k);
scanf("%s",s);
char ch = ;
char ans[] = "";
for(int i = ;s[i + k - ];i ++) {
swap(s[i + k],ch);
int d = atoi(s + i);
if(ispri(d)) {
strcpy(ans,s + i);
break;
}
swap(s[i + k],ch);
}
puts(ans);
return ;
}

pat甲级 1152 Google Recruitment (20 分)的更多相关文章

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

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

  2. 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 ...

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

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

  4. 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 ...

  5. PAT 甲级 1035 Password (20 分)

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

  6. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  7. PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)

    1050 String Subtraction (20 分)   Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be t ...

  8. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  9. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

随机推荐

  1. 简单UML

    http://www.cnblogs.com/pluviophile/p/7542017.html,看了这篇博文感觉这个例子有用遂搬运过来方便查阅 上面的类图就体现了所需要了解的类的6个基本关系 该类 ...

  2. mock数据

    作为前端经常需要模拟后台数据,我们称之为mock.通常的方式为自己搭建一个服务器,返回我们想要的数据. 在这里我们使用node.js来实现 http://www.cnblogs.com/bsn-hua ...

  3. 场景设计以及Manual Scenario和Goal-OrientedScenario的区别

    Manual Scenario 手工场景 主要是设计用户变化,通过手工场景可以帮助我们分析系统的性能瓶颈.手动方案:如果要生成手动方案,请选择此方法.通过创建组并指定脚本.负载生成器和每组中包括的 V ...

  4. 修改linux系统用户最大线程数限制

    linux系统对线程数量有个最大限制,当达到系统限制的最大线程数时使用账号密码ssh到系统时是无法登陆的,会报Write failed: Broken pipe,或者是shell request fa ...

  5. Python之路,Day9 - 线程、进程、协程和IO多路复用

    参考博客: 线程.进程.协程: http://www.cnblogs.com/wupeiqi/articles/5040827.html http://www.cnblogs.com/alex3714 ...

  6. 028——VUE中事件修饰符once

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【nyoj-1233】差值

    描述 输入一个整数数组,将它们连接起来排成一个数,找出能排出的所有数字中最大,最小的两个,输出两个数的差值.例如输入数组{1,  2},则输出9.     输入 第一行输入一个整数T,表示有T组测试数 ...

  8. js 多个倒计时,毫秒倒计时

    其实主要是借鉴了了这篇文的写法(http://tuzwu.iteye.com/blog/819081),俺稍作了修改,以便更适合我的需要: 实现功能:调用一个函数,传入html元素的id,和一个截止时 ...

  9. Linux系统下超级用户密码的修改

    1)重启系统:在虚拟机刚启动界面,不停地按上下键,停止系统的自动引导(界面底部有提示) 2) 按 e 进入编辑模式 3) 编辑内容如下:完成后按Ctrl+x    (具体编辑内容为下图:删除倒数第三行 ...

  10. Linux文件在系统中传输

    一.文件的传输 1.命令:scp scp file user@ip:/dir ##把当前系统目录下的文件file复制到另一个系统目录下 scp user@ip:/file dir 2.命令:rsync ...