题目描述


分析:截取字符串,判断是否是素数
C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)
#include<string>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
bool isPrime(ll n){
if(n<2){
return false;
}
for(ll i=2;i<=(ll)sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll L,K;
string str;
cin>>L>>K>>str;
for(int i=0;i<str.length()-K+1;i++){
string temp=str.substr(i,K);
if(isPrime(stoll(temp))){
cout<<temp<<endl;
return 0;
}
}
cout<<"404"<<endl;
}
本文介绍了一个使用C++实现的程序,该程序能够从输入的字符串中截取出指定长度的子串,并判断这些子串所代表的数字是否为素数。通过分析代码,我们了解到如何利用C++标准库进行字符串到整数的转换,以及如何实现高效的素数检测算法。
5716

被折叠的 条评论
为什么被折叠?



