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. [bzoj1047][HAOI2007]理想的正方形_动态规划_单调队列

    理想的正方形 bzoj-1047 HAOI-2007 题目大意:有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 注释:$2\le a, ...

  2. BZOJ——T 1801: [Ahoi2009]chess 中国象棋

    http://www.lydsy.com/JudgeOnline/problem.php?id=1801 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit:  ...

  3. TRIZ的成功案例

    这篇採訪稿是几年前的,是TRIZ成功案例离我近期的,由于主人公是我的朋友,请点击查看: 培训后技术难题就攻克了 后记:学了TRIZ并不能让您100%解决您全部遇到的问题,但这样的思想和方法确实是最具操 ...

  4. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

  5. CreateDialog Win32 API调用的一个小问题

    在老版本号的VC编译器上.关键调用是下面2句: InitCommonDialogs(); HWND hwndDialog = CreateDialog(hInstance, "IDD_XXX ...

  6. Unable to instantiate Action, xxxAction, defined for &#39;xxx&#39; in namespace &#39;/&#39;xxxAction解决方式

    出现这个问题的解决办法主要有两个 1.假设项目没有使用Spring,则struts.xml配置文件里,这个action的class属性的路径没有写完整,应该是包名.类名 2.假设项目使用了Spring ...

  7. SolrCloud 分布式集群部署步骤

    https://segmentfault.com/a/1190000000595712 SolrCloud 分布式集群部署步骤 solr solrcloud zookeeper apache-tomc ...

  8. case when in sql server's stored procedure

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql Evaluates a list of c ...

  9. Extension Methods (C# Programming Guide)

    https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...

  10. C++调用shell脚本

    调用函数时候,传入脚本路径名称或者具体命令. int shell_call(std::string &cmdstr) { }; char line[maxline]; FILE *fpin; ...