特殊的质数肋骨
題意
從右邊開始切下肋骨,每次還剩下的肋骨上的數字都組成一個質數,舉例來說: 7 3 3 1 全部肋骨上的數字 7331是質數;三根肋骨 733是質數;二根肋骨 73 是質數;當然,最后一根肋骨 7 也是質數。 7331 被叫做長度 4 的特殊質數。寫一個程序對給定的肋骨的數目 N (1<=N<=8),求出所有的特殊質數。
分析
每個質數肋骨的首位一定是2、3、5、7之一;往后的每一位一定是1、3、7、9之一。
var
n:longint;
function ss(w:string):longint;
var
sz,i:longint;
begin
? ? val(w,sz);
? ? if sz=1 then exit(1);
? ? for i:=2 to trunc(sqrt(sz)) do
? ? if sz mod i=0 then exit(1);
? ? exit(0);
end;
procedure try(s:string);
begin
? ? if length(s)=n then
? ? begin
? ? ? ? writeln(s);
? ? ? ? exit;
? ? end;
? ? if ss(s+'1')=0 then try(s+'1');
? ? if ss(s+'3')=0 then try(s+'3');
? ? if ss(s+'7')=0 then try(s+'7');
? ? if ss(s+'9')=0 then try(s+'9');
end;
begin
? ? read(n);
? ? try('2');
? ? try('3');
? ? try('5');
? ? try('7');
end.
轉載于:https://www.cnblogs.com/YYC-0304/p/9500194.html
總結