【1152】Google Recruitment (20 分)
        【摘要】 
                    #include<iostream>#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string>#include<algorithm>  #include<map>#include...
    
    
    
    
  
   - 
    
     
    
    
     
      #include<iostream>
     
    
 
   - 
    
     
    
    
     
      #include<stdio.h>
     
    
 
   - 
    
     
    
    
     
      #include<stdlib.h>
     
    
 
   - 
    
     
    
    
     
      #include<math.h>
     
    
 
   - 
    
     
    
    
     
      #include<string>
     
    
 
   - 
    
     
    
    
     
      #include<algorithm> 
     
    
 
   - 
    
     
    
    
     
      #include<map>
     
    
 
   - 
    
     
    
    
     
      #include<vector>
     
    
 
   - 
    
     
    
    
     
      #include<queue> 
     
    
 
   - 
    
     
    
    
     
      using namespace std;  
     
    
 
   - 
    
     
    
    
     
      //在常数e中枚举连续长度为20的素数
     
    
 
   - 
    
     
    
    
     
      //key:stoi字符串转数字 substr()截取字符子串
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      bool isPrime(int n){  //判断n是否为素数
     
    
 
   - 
    
     
    
    
     	if(n<=1) return false;
     
    
 
   - 
    
     
    
    
     	int sqr=(int)sqrt(1.0*n); //sqrt的作用是位一个浮点数开根号
     
    
 
   - 
    
     
    
    
     	for(int i=2;i<=sqr;i++){  //遍历2到根号n
     
    
 
   - 
    
     
    
    
     		if(n % i == 0)  return false;
     
    
 
   - 
    
     
    
    
     
      	}
     
    
 
   - 
    
     
    
    
     	return true; //n是素数
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
     
      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);
     
    
 
   - 
    
     
    
    
     		int num=stoi(t);  //此处用stod也可以
     
    
 
   - 
    
     
    
    
     		if(isPrime(num)){
     
    
 
   - 
    
     
    
    
     
      			cout << t;
     
    
 
   - 
    
     
    
    
     			return 0;
     
    
 
   - 
    
     
    
    
     
      		}
     
    
 
   - 
    
     
    
    
     
      	}
     
    
 
   - 
    
     
    
    
     
      	cout  << "404\n";
     
    
 
   - 
    
     
    
    
     	system("pause");
     
    
 
   - 
    
     
    
    
         return 0;   
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
文章来源: andyguo.blog.csdn.net,作者:山顶夕景,版权归原作者所有,如需转载,请联系作者。
原文链接:andyguo.blog.csdn.net/article/details/99414508
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)