1152 Google Recruitment (20 分)

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

判断素数
 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 using namespace std;
 4 string s;
 5 int n,m;
 6
 7 bool isprime(ll x){
 8     if(x == 0 || x == 1)
 9         return false;
10     if(x == 2 || x == 3)
11         return true;
12     bool prime = true;
13     for(ll i = 2; i*i <= x; i++){
14         if(x%i == 0){
15             prime = false;
16             break;
17         }
18     }
19     return prime;
20 }
21
22 int main(){
23     cin >> m >> n >> s;
24     ll an = 0;
25     if(m < n || n == 0){
26         cout <<"404"<<endl;
27         return 0;
28     }
29     for(int i = 0; i < n; ++i){
30         an = an*10 +  s[i] - ‘0‘;
31     }
32     int mod = 1;
33     for(int i = 1; i < n; ++i) mod *= 10;
34     for(int i = n; i < m; ++i){
35         if(!isprime(an)){
36             an = an%mod;
37             an = an*10 + s[i] - ‘0‘;
38         }else{
39             break;
40         }
41     }
42     if(isprime(an)){
43         string ss="";
44         while(n--){
45             ss += an%10+‘0‘;
46             an /= 10;
47         }
48         reverse(ss.begin(), ss.end());
49         cout << ss << endl;
50     }else{
51         cout << "404" << endl;
52     }
53     return 0;
54 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11267222.html

时间: 2024-10-17 10:18:44

1152 Google Recruitment (20 分)的相关文章

PAT Advanced 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 co

PAT Advanced 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 co

1152 Google Recruitment

多年不练截取string用substr都忘了,还有要记住最后是要输出整一条string,比如,应该0023,而不是23,一开始一直不对... #include <iostream> #include <stdlib.h> #include <string> #include<algorithm> #include<vector> #include<cmath> using namespace std; int isprime(int

PTA-C-4-7 统计某类完全平方数 (20分)

4-7 统计某类完全平方数   (20分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144.676等. 函数接口定义: int IsTheNumber ( const int N ); 其中N是用户传入的参数.如果N满足条件,则该函数必须返回1,否则返回0. 裁判测试程序样例: #include <stdio.h> #include <math.h> int IsTheNumber ( const int N ); int ma

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样

PTA 邻接表存储图的广度优先遍历(20 分)

6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ); 其中LGraph是邻接表存储的图,定义如下: /* 邻接点的定义 */ typedef struct AdjVNode *PtrToAdjVNode; struct AdjVNode{ Vertex AdjV; /* 邻接点下标 */ PtrToAdjVNode Next; /*

6-1 单链表逆转(20 分)

6-1 单链表逆转(20 分) 本题要求实现一个函数,将给定的单链表逆转. 函数接口定义: List Reverse( List L ); 其中List结构定义如下: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; typedef PtrToNode List; /* 定义单链表类型 */ L是给定单链表,函数Rever

6-2 顺序表操作集(20 分)

6-2 顺序表操作集(20 分) 本题要求实现顺序表的操作集. 函数接口定义: List MakeEmpty(); Position Find( List L, ElementType X ); bool Insert( List L, ElementType X, Position P ); bool Delete( List L, Position P ); 其中List结构定义如下: typedef int Position; typedef struct LNode *List; str

7-4 找出不是两个数组共有的元素(20 分)

7-4 找出不是两个数组共有的元素(20 分) 给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在两行中给出两个整型数组,每行先给出正整数N(≤20),随后是N个整数,其间以空格分隔. 输出格式: 在一行中按照数字给出的顺序输出不是两数组共有的元素,数字间以空格分隔,但行末不得有多余的空格.题目保证至少存在一个这样的数字.同一数字不重复输出. 输入样例: 10 3 -5 2 8 0 3 5 -15 9 100 11 6 4 8 2 6 -5 9 0 100 8 1 输出样