pat甲级 1152 Google Recruitment (20 分)
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 分)的更多相关文章
- PAT甲级——1152.Google Recruitment (20分)
1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...
- 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 ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- 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 ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be t ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT 甲级 1042 Shuffling Machine (20 分)(简单题)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
随机推荐
- kibana 安装
一 介绍 kibana 主要实现对日志的可视化显示. 二 安装 下载安装包: wget https://download.elastic.co/kibana/kibana/kibana-4.1.2-l ...
- Redis-CentOS7安装
安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: 第一步:redis的源码包上传到linux系统. ...
- flask学习(八):页面跳转和重定向
1. 用处:在用户访问一些需要登录的页面的时候,如果用户没有登录,那么让页面重定向到登录页面 2. 实例 运行效果: 用户已登录,进入发布问答页面 用户未登录,跳转到登录页面
- 《OpenCL编程指南》之 与Direct3D互操作
介绍OpenCL与D3D 10之间的互操作. 1.初始化OpenCL上下文实现Direct3D互操作 OpenCL共享由pragma cl_khr_d3d10_sharing启用: #pragma O ...
- win32调用系统颜色对话框
参考网站:http://blog.csdn.net/u013242177/article/details/50437358 首先要包含commdlg.h头文件,这个是通用对话框的头文件,包括文件对话框 ...
- 河南省多校联盟二-C
1281: 邪能炸弹 时间限制: 1 秒 内存限制: 128 MB提交: 222 解决: 80 题目描述 正在入侵艾泽拉斯的古尔丹偶然间得到了一颗邪能炸弹,经过研究,他发现这是一颗威力极其巨大且难 ...
- 原生javascript-分享自己常用的函数
[一]添加监听事件 addHandler:function(node,type,fn){if(node.addEventListener){ node.addEventListener(type,fn ...
- 这可能是最详细的 iOS 学习入门指南(含书目/文档/学习资料)
1 零基础小白如何进行 iOS 系统学习 首先,学习目标要明确: 其次,有了目标,要培养兴趣,经常给自己一些正面的反馈,比如对自己的进步进行鼓励,在前期小步快走: 再次,学技术最重要的一点就是多动手. ...
- oracle 修改字符集 修改为ZHS16GBK
oracle数据库的字符集更改 A.oracle server 端 字符集查询 select userenv('language') from dual 其中NLS_CHARACTERSET 为ser ...
- lspci能看到ifconfig -a看不到网卡
随着宽带技术的快速发展,服务器使用万兆网卡的概率越来越高.最近装了几台服务器都用的万兆网卡,为了图便宜,网卡和模块都是淘宝上买的,这部还真遇到不少问题. 我的服务器都是centos6.4 64位的,网 ...