码迷,mamicode.com
首页 > 其他好文 > 详细

1152 Google Recruitment (20 分)

时间:2019-07-30 00:44:23      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:https   const   amp   while   seve   must   justify   code   素数   

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 }

 




1152 Google Recruitment (20 分)

标签:https   const   amp   while   seve   must   justify   code   素数   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!