当前位置:网站首页>PAT甲级1152 Google Recruitment (20 分)
PAT甲级1152 Google Recruitment (20 分)
2022-07-25 15:24:00 【nekoha_dexter】
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 523654987725541023819Sample Output 1:
49877Sample Input 2:
10 32468024680Sample Output 2:
404题目大意:
给出长度为L数字,求第一个k位长的素数。如果找不到输出404.前导0也算入数位。
思路:
不用建树,用 前序+中序转层序的办法,每一次都等读到一个根,如果a 和 用string存储,用stoi()转化为整数去判断是否为素数。
参考代码:
#include<string>
#include<iostream>
using namespace std;
string s;
int l, k;
bool isprime(int v){
if(v <= 1) return false;
for(int i = 2; i * i < v; ++i)
if(v % i == 0) return false;
return true;
}
int main(){
scanf("%d%d", &l, &k);
cin >> s;
for(int i = 0; i <= l - k; i++){
int temp = stoi(s.substr(i, k));
if(isprime(temp)){
cout << s.substr(i, k);
return 0;
}
}
printf("404");
return 0;
}
边栏推荐
猜你喜欢

ML - 语音 - 深度神经网络模型

MySQL transactions and mvcc

Application of object detection based on OpenCV and yolov3

记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Idea remotely submits spark tasks to the yarn cluster

ML - 语音 - 传统语音模型

Node learning

Take you to create your first C program (recommended Collection)
随机推荐
IOS interview questions
Spark提交参数--files的使用
ZOJ - 4114 Flipping Game-dp,合理状态表示
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
C#精挑整理知识要点12 异常处理(建议收藏)
Xcode added mobileprovision certificate file error: Xcode encoded an error
单例模式3--单例模式
ML - 语音 - 语音处理介绍
二进制补码
Yan required executor memory is above the max threshold (8192mb) of this cluster!
The difference between Apple buy in and apple pay
ML - 语音 - 传统语音模型
Spark AQE
苹果内购和Apple Pay 的区别
VMware Workstation fails to start VMware authorization service when opening virtual machine
Spark获取DataFrame中列的方式--col,$,column,apply
JVM dynamic bytecode technology details
Spark DF增加一列
CF888G-巧妙字典树+暴力分治(异或最小生成树)
Spark AQE