PAT甲级——A1152 GoogleRecruitment【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
Solution:
这道题令我惊讶的是,竟然是简单的判断一下是不是素数?!
本以为这么大的数字级别,应该是建立素数表来判断的,想不到竟然时一个个数字进行简单的判断是不是素数?
倒是建立素数表内存超了,判断是不是素数竟然没有超时?!
下面代码给出了建立素数表
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int n, k;
string str, res = "";
//void getPrimeTable(int inf, vector<bool>¬Prime)//创建素数表
//{
// notPrime[0] = notPrime[1] = true;
// for (int i = 2; i <= inf; ++i)
// if (notPrime[i] == false)//从2这个素数开始
// for (int j = 2; j*i <= inf; ++j)
// notPrime[j*i] = true;//剔除素数的所有倍数
//} bool isPrime(int x)//判断是不是素数
{
if (x < )
return true;
for (int i = ; i*i <= x; ++i)
if (x%i == )
return false;
return true;
}
int main()
{
cin >> n >> k;
cin >> str;
//int size = (int)pow(10, k);
//vector<bool>notPrime(size+1, false);//防止内存太大,我这里是动态建立数组的
//getPrimeTable(size, notPrime);//创建素数表
for (int i = ; i + k <= n; ++i)
{ string s = str.substr(i, k);
int num = atoi(s.c_str());
if (isPrime(num))//notPrime[num]==false)//使用的代码简单的素数判断,注释的代码是使用素数表
{
res = s;
break;
}
}
if (res.size() > )
cout<<res;
else
cout << "";
return ;
}
PAT甲级——A1152 GoogleRecruitment【20】的更多相关文章
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- PAT甲级——1035 Password (20分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- PAT 甲级 1008 Elevator (20)(代码)
1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)
1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...
- PAT 甲级 1008 Elevator (20)(20 分)模拟水题
题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...
- PAT甲级——1061 Dating (20分)
Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...
- PAT甲级——1005.SpellItRight(20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- PAT甲级——1077.Kuchiguse(20分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
随机推荐
- mongodb写入策略(WriteConcern)
写入策略(WriteConcern) mongodb的写入策略有多种方式,写入策略是指当客户端发起写入请求后,数据库什么时候给应答,mongodb有三种处理策略:客户端发出去的时候,服务器收到请求的时 ...
- 实用|从0到1 搭建Web性能监控系统
工具介绍 1. Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理. 常见支持的「 ...
- mybatis 学习视频总结记录
学习mybaits简单增删改查例子记录 此整理是学习视频后的视频内容整理,后半段还没有整理 网易云课堂 SSM高级整合视频 地址 : http://study.163.com/course/cours ...
- CSL 的密码(后缀数组)
CSL 的密码 题目传送门 解题思路 后缀数组.对于每一个后缀\(k\)都有\(n - k + 1\)个前缀,把所有不和前一个(排序后的)公共且长度大于\(m\)的前缀个数加起来. 代码如下 #inc ...
- CF1241 D Sequence Sorting(离散化+DP)
题意: 给定数组a[n],用两种操作: 1.将数组中所有值为x的数移至开头 2.将数组中所有值为x的数移至末尾 问,经过最少多少次操作,能将数组a[n]变为非递减的有序数列? (1<=n< ...
- golang的数据类型之字符类型
字符类型使用细节 1)字符常量是用单引号('')括起来的单个字符.例如:var c1 byte = 'a' var c2 int = '中' var c3 byte = '9' 2) Go中允许使用转 ...
- JavaScript 标准参考教程(alpha) 阮一峰
JavaScript 标准参考教程(alpha)http://javascript.ruanyifeng.com/#introduction
- mysql数值字符串类型的按照数值进行排序
今天遇到一个问题,就是对mysql数值字符串类型进行排序,在默认情况下使用order by 字段名称 desc/asc 进行排序的时候,mysql进行的排序规则是按照ASCII码进行排序的,并不会自动 ...
- OKVIS框架之前端
1. 数据流入 在okvis_app_sychronous.cpp内,把IMU和图像数据加入到各自的队列里.由ThreadedKFVio负责队列的各种操作.作者对队列加了特殊功能,保证队列是线程安全的 ...
- MySQLSyntaxErrorException: Row size too large 转摘自:https://confluence.atlassian.com/display/CONFKB/MySQLSyntaxErrorException%3A+Row+size+too+large
Symptoms The following appears in the atlassian-confluence.log: Caused by: com.mysql.jdbc.exceptions ...