一个整数的整数子串是由该整数的连续若干位的数字构成。例如:6158的子串包括6,l,5,8,61,15,58,615,158,6158,其中质数子串有:5,61。你的任务是找出其中最大的质数子串,说明:0,1不是质数。
[输入格式]
键盘输人整数N(O≤N≤l OOO OOO OOOO)。
[输出格式]
屏幕输出N的最大质数子串,若所有子串都是非质数,则输出“No primes”。
[输入样例1]
319
[输出样例1]
31
[输入样例2]
6804
[输出样例2]
No primes
const
maxn=9;
p:array[0..9] of longint=(1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000);
var
n,len,m,i,k,ans:longint;
a:array[1..maxn+1] of longint;
function prime(a:longint):boolean;
var i,j:longint;
begin
if (a=0)or(a=1) then begin prime:=false; exit; end;
j:=trunc(sqrt(a));
for i:=2 to j do
if a mod i=0 then begin prime:=false; exit; end;
① ;
end;
begin
readln(n);
len:=trunc(ln(n)/ln(10))+1;
for i:=1 to len do
begin
a[i]:=n mod 10; n:=n div 10;
end;
for k:=len downto 1 do
begin
ans:=-1; m:=0;
for i:=k downto 1 do m:=m*10+a[i];
if ② then ans:=m;
for i:=k+1 to len do
begin
③ ;
if prime(m)and(m>ans) then ans:=m;
end;
if ④ then begin writeln(ans); break; end;
end;
if ans=-1 then writeln('No primes');
end.
【答案】
①prime:=true
②prime(m)and(m>ans)
③m:=m div 10+a[i]*p[k-1]
④ans<>-1
const maxn=9; p:array[0..9] of longint=(1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000); var n,len,m,i,k,ans:longint; a:array[1..maxn+1] of longint; function prime(a:longint):boolean; var i,j:longint; begin if (a=0)or(a=1) then begin prime:=false; exit; end; j:=trunc(sqrt(a)); for i:=2 to j do if a mod i=0 then begin prime:=false; exit; end; prime:=true; end; begin readln(n); len:=trunc(ln(n)/ln(10))+1; for i:=1 to len do begin a[i]:=n mod 10; n:=n div 10; end; for k:=len downto 1 do begin ans:=-1; m:=0; for i:=k downto 1 do m:=m*10+a[i]; if prime(m)and(m>ans) then ans:=m; for i:=k+1 to len do begin m:=m div 10+a[i]*p[k-1]; if prime(m)and(m>ans) then ans:=m; end; if ans<>-1 then begin writeln(ans); break; end; end; if ans=-1 then writeln('No primes'); end.