PATA1152 题解

PATA1152 题解

题意分析:

给定一个数字字符串(我都不知道这个L输入有什么用。。。),在字符串中任意选取一个长度为k的字串,判断它是否为可能的素数。如果有,请输出第一个这样的字串;若没有,输出404

注意的点:

这里要注意,这道题需要把0给输出,所以遇到0我们不能直接continue,而是要更新字符串,然后再进行筛选。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
int length,k;
string str,ans;
bool exist = false;
ll str_to_ll(const string &str){
    ll ans = 0;
    for(int i = 0;i<str.length();i++){
        ans = ans * 10 + str[i] - '0';
    }
    return ans;
}

bool isPrime(ll num){
    if(num <= 1) return false;
    int sqr = (ll)sqrt(num);
    for(int i = 2;i<=sqr;i++){
        if(num % i == 0){
            return false;
        }
    }
    return true;
}   

int main(){
    cin>>length>>k>>str;
    for(int i = 0;i<length-k+1;i++){
        //if(str[i] == '0') continue;
        ans = str.substr(i,k);
        int num = str_to_ll(ans);
        if(isPrime(num)){
            exist = true;
            break;
        }
    }
    if(exist){
        cout<<ans<<endl;
    }
    else{
        cout<<"404"<<endl;
    }
    return 0;
}

总结:

水题,没啥好说的。难度和输入输出差不了太多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值