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

这题是字符串的知识,故最近更新字符串的题目。

参考的柳婼的代码:

#include <iostream>
#include <string>
using namespace std;
bool isPrimeNumber(int a) // 判断是否是素数
{
    if(a == 0||a == 1) //首先排除0和1
        return false;
    for(int i = 2; i * i <= a; i++)//再次排除其他数
        if(a % i ==0)
            return false;
    return true;
}

int main() {
    int L,K;
    string s;
    cin >> L >> K >> s;
    for(int i = 0; i <= L-K; i++) {
        string t = s.substr(i, K); 		//截取字符串从 i 到i + K
        int num = stoi(t);			//将截取的字符串转化为数字
        if (isPrimeNumber(num)) {		//判断是否是素数
            cout << t;
            return 0;
        }
    }
    cout << "404\n";
    return 0;
}

题解中主要用到了 string 下的 substr 与 stoi 两个函数:

substr

用于截取字符串的某一段,用法如下:

string substr (size_t pos = 0, size_t len = npos) const;

Returns a newly constructed string object with its value initialized to a copy of a substring of this object.

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

stoi

int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);

Convert string to integer

将字符串转化为整型

Parses str interpreting its content as an integral number of the specified base, which is returned as an int value.

If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.

The function uses strtol (or wcstol) to perform the conversion (see strtol for more details on the process).

类似的字符串转换成其他的函数还有

函数名 含义
stod Convert string to double
stof Convert string to float
stoi Convert string to int
stol Convert string to long int
stold Convert string to long double
stoll Convert string to long long
stoul Convert string to unsigned integer
stoull Convert string to unsigned long long
to_string Convert numerical value to string
to_wstring Convert numerical value to wide string

等,含义其实都是一个类型。将 string 转化为…

PAT甲级——1152.Google Recruitment (20分)的更多相关文章

  1. PAT Advanced 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  2. PAT甲级:1152 Google Recruitment (20分)

    PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...

  3. pat甲级 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  4. PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  5. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  6. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  7. PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)

    1050 String Subtraction (20 分)   Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be t ...

  8. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  9. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

随机推荐

  1. js运用sort对json 数组进行排序

    Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列.sort() 方法可以接受一个 方法为参数. sort()排序时每次比较两个数组项都回执行这个参数,并把两个比较的数组 ...

  2. cf 609E.Minimum spanning tree for each edge

    最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).( ...

  3. SQL中将某个字段根据分隔符分割成两个字段

    假设有表如下: 商品名称 耐克/DS001 安踏/AT002 阿迪达斯/AD009 使用SQL将[商品名称]字段按照分隔符‘/’进行分割后实现以下效果: 品牌 商品代码 耐克 DS001 安踏 AT0 ...

  4. swift之保存数据到keychain

    访问KeyChain 1.在mac上按下 Command+Space 输入Keychain Access 2.在终端输入security find-generic-password -help 读取配 ...

  5. Adaboost的python实现

    不要总是掉包欧,真的丢人啊,一起码起来! '''函数的功能:单层决策树分类函数参数说明: xMat:数据矩阵 i:第i列,第几个特征 Q:阈值返回分类结果: re'''import numpy as ...

  6. Tomcat解压版Windows配置(运行环境非开发环境)

    tomcat官网下载的9.0.19,解压后目录如下: java官网下载的jre8 (8u131),目录如下(应该是下载的解压版): 打开tomcat9.0.19根目录下的RUNNING.txt,里面有 ...

  7. 使用 this 关键字定义方法和属性

    1.方法和属性的定义 属性是类中声明的变量,与其他地方变量的声明基本相同,只是属性必须 this 关键字,并且这里没有var 关键字. this.age; 在使用时,先是 this 关键字,之后的点语 ...

  8. Dlib笔记二:matrix或array2d与cv::Mat的互转

    因为经常习惯的用OpenCV来做图像处理,所以难免希望将其他库的图像数据与OpenCV互转,所以今天就记录下这种互转的方法. 1.dlib::matrix/dlib::array2d转cv::Mat ...

  9. 应用层上的协议HTTP

    HTTP http://www.runoob.com/http/http-tutorial.html https://www.cnblogs.com/houfee/articles/9161847.h ...

  10. Feign整合测试

    1.测试使用 (1)服务调用方引入依赖 <dependency> <groupId>org.springframework.cloud</groupId> < ...