[TOJ 4705] maximum prime number, toj4705 Prime Number
Description
Given a number of n, I asked if we could extract several consecutive bits from the order from left to right and combine them into a prime number (greater than 1, and can only be divided by 1 and itself is called a prime number), if there are multiple possibilities, take the maximum of all possible numbers.
For example, in 1234, we can take 1234,123,234, 4, and find the prime numbers are 2, 3, 23, and the maximum prime number is 23.
Input
There are multiple groups of input data. Each group occupies one row and each row has a positive integer of n (2 <= n <= 2147483647 ).
Output
For each output group, if yes, the maximum prime number is output; otherwise, None is output.
Sample input
1234
Sample output
23
Note: When the number of digits is the same, pay attention to the prime number of the maximum value !!
#include<iostream> #include<cstring>#include<cmath>#include<algorithm>#include<sstream>using namespace std;int prime(int a){ int i; if(a==1||a==0) return 1; for(i=2;i<=sqrt(a);i++) { if(a%i==0) return 1; } return 0;}int ok(int m){ int i,s=1; for(i=1;i<=m;i++) s=s*10; return s;}int weishu(int a){ int s=0; while(a!=0) { a=a/10; s++; } return s;}int main(){ int i,j,t,n,ws,k,p,a,maxx; while(scanf("%d",&n)!=EOF) { t=ws=weishu(n); //cout<<"ws="<<ws<<endl; k=n; maxx=-1; for(i=1;i<=ws;i++) { if(t==10) { if(prime(k)==0) { maxx=k; cout<<k<<endl; break; } t--; } else { p=ok(t); t--; //cout<<"i="<<i<<" p="<<p<<endl; k=n; for(j=1;j<=i;j++) { //cout<<"k="<<k<<endl; if(prime(k%p)==0) { a=k%p; maxx=max(a,maxx); } k=k/10; } if(maxx!=-1) { cout<<maxx<<endl; break; } } if(maxx!=-1)break; } if(maxx==-1)cout<<"None"<<endl; } return 0;}