1152 Google Recruitment

题干前半略。

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<stdio.h>
#include<vector>
#include<string>
#include<iostream>
#include<math.h>
using namespace std;

bool isprime(int n)
{
    
    if(n==1||n==0) return false;
    for(int i=2;i<=(int)sqrt(n);i++)
    {
        if(n%i==0) return false;
    }
    return true;
}
bool flag;
int main()
{
    int num;
    int primenum;
    scanf("%d",&num);
    scanf("%d",&primenum);
    string seq;
    cin>>seq;
    while(seq[0]=='0') seq.erase(seq.begin());
    int i;
    for(i=0;i<num;i++)
    {
        int start=i;
        char temp[20];
        int t=0;
        if(num-i<primenum) break;
        for(int j=i;j<i+primenum;j++)
        {
            temp[t++]=seq[j];
        }
        int newnum;
        sscanf(temp,"%d",&newnum);
        temp[t]='\0';
        if(isprime(newnum)) 
        {
            cout<<temp;
            flag=true;
            return 0;
        }
    }
    if(i==num-primenum+1) printf("404\n");
}

 

posted @ 2020-03-21 21:40  zzzlight  阅读(204)  评论(0编辑  收藏  举报