特殊的质数肋骨

    xiaoxiao2021-03-26  109

    题意

    从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。 7331 被叫做长度 4 的特殊质数。写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数。

    分析

    每个质数肋骨的首位一定是2、3、5、7之一;往后的每一位一定是1、3、7、9之一。

    varn:longint;function ss(w:string):longint;varsz,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://ju.6miu.com/read-661478.html

    最新回复(0)