PAT_A1152#Google Recruitment
Source:
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的更多相关文章
- PAT 1152 Google Recruitment
1152 Google Recruitment (20 分) In July 2004, Google posted on a giant billboard along Highway 101 ...
- PAT甲级——1152.Google Recruitment (20分)
1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...
- PAT-1152(Google Recruitment)字符串+素数
Google Recruitment PAT-1152 本题最需要注意的是最后输出要以字符串形式输出,否则可能会出现前导0的情况. /** * @Author WaleGarrett * @Date ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- pat甲级 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- 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 (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 ...
- 1152 Google Recruitment
题干前半略. Input Specification: Each input file contains one test case. Each case first gives in a line ...
随机推荐
- mysql 源码 王恒 [mysql数据结构+mysql 执行计划]
http://blog.chinaunix.net/uid/26896862/cid-156733-list-1.html http://www.it168.com/redian/wangheng/
- JDBC数据源(DataSource)数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用。
JDBC数据源(DataSource)的简单实现 数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用. 2.数据源提供了一种简单获取数据库连接的方式,并能在内部通 ...
- IBM CEO罗睿兰:科技公司屹立百年的3个秘诀
假设有不论什么科技公司能够完美阐释"转型"这个词的含义,那么这家公司非创立103年的IBM莫属. 如今,它的变化更胜以往. 在<財富>杂志周二于美国加利福尼亚州拉古纳尼 ...
- 为什么要阅读——兼分享《首先,打破一切常规》[中译文]:世界顶级管理者的成功秘诀/(美)马库斯·白金汉,(美)柯特·科夫曼 著
<ctrlno=255632">首先,打破一切常规>[中译文]:世界顶级管理者的成功秘诀/(美)马库斯·白金汉,(美)柯特·科夫曼 著:鲍世修 等译 下载地址:http:/ ...
- jenkins下载插件失败解决办法
- 9.JAVA编程思想 多形性
欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/51040241 "对于面向对象的程序设计语言,多型性是第三种最主要的特 ...
- Java:Socket通信
Socket通常也称作"套接字".用于描写叙述IP地址和port,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ServerS ...
- WCF学习笔记——不支持内容类型 text/xml; charset=utf-8
我在使用WCF的时候,客户端运行报错: 不支持内容类型 text/xml; charset=utf-8 原因是WCF服务做了修改.刷新客户端的服务引用,问题消失 =================== ...
- oc37--类工厂方法
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...
- poi读取docx中的文字和图片(自己应用)
poi读取docx中的文字和图片(自己应用) package com.fry.poiDemo.dao; import java.io.File; import java.io.FileInputStr ...