PAT甲级 1152 Google Recruitment (20分) 字符串处理/C++
2020-06-24 04:26
696 查看
1152 Google Recruitment (20分)
字符串处理问题
题目的大意:输入长度为n的一串字符,寻找下标最小的长度为k的素数字符串
思路:从字符串的头开始,逐个切割k个单位的字符,使用stoi()函数,将string转化为int型,再判断是否为素数
#include<iostream> //输入输出流头文件 #include<math.h> //数学函数 #include<string> //C++string类 using namespace std; //标准命名空间 void findPrime(string s,int k,int n); int isPrime(int x); int main(){ //主函数 #ifdef ONLINE_JUDGE //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入 #else freopen("1.txt", "r", stdin); //从1.txt输入数据 #endif int length,k; cin>>length>>k; string s; cin>>s; findPrime(s,k,length); return 0; //返回0,如果不返回0,PAT会报错 } void findPrime(string s,int k,int n){ int i=0; while(i<=n-k){//到最后的k个字符单位,不用继续往后 string s1=s.substr(i,k); int x=stoi(s1); if(isPrime(x)) { cout<<s1<<endl; return; } i++; } cout<<"404"<<endl; return ; } int isPrime(int x){ if(x<=1) return 0; for(int i=2;i<=sqrt((double)x);i++){ if(x%i==0) return 0; } return 1; }
相关文章推荐
- C++ PAT甲级1152 Google Recruitment (20分)
- PAT 甲级 1100 Mars Numbers (20分) 字符串处理/C++
- PAT甲级 1140 Look-and-say Sequence (20分) 字符串处理/C++
- PAT 甲级 1152 Google Recruitment
- 1152 Google Recruitment (20 分)--PAT甲级
- PAT - 甲级 - 1035. Password (20)(字符串处理)
- C++ PAT甲级 1008 Elevator (20分)
- (PAT Basic)1031 查验身份(字符串处理) C++
- PAT-A-1071 Speech Patterns (25 分) 字符串处理,transform、istringstream的使用 C++题解
- PAT - 甲级 - 1082. Read Number in Chinese (25)(字符串处理)
- PAT甲级 1108 Finding Average (20分) C++
- PAT甲级字符串处理1001 A+B Format
- C++ PAT 甲级1108 Finding Average (20分)
- C++ PAT甲级 1005 Spell It Right (20分)
- C++ PAT甲级 1104 Sum of Number Segments (20分)
- C++ PAT甲级 1001 A+B Format (20分)
- PAT - 甲级 - 1061. Dating (20)(字符串处理)
- C++ PAT甲级 1096 Consecutive Factors (20分)
- C++ PAT 甲级 1100 Mars Numbers (20分)
- PAT甲级字符串处理1061 Dating