作业帮 > 综合 > 作业

pascal题目现在给你N个0~9的数字并排成一列,同时还给出了一个取数长度L.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/19 23:36:04
pascal题目现在给你N个0~9的数字并排成一列,同时还给出了一个取数长度L.
规定先从第一个数字开始从左往右连续取L个数字,拼成一个产度为L位(最高位为0的L-1位数除外)的数,然后从第2个数字开始从左往右连续取L个数字.,这样,最后最多可以得到N-L+1个L位数.现在请你将这些L位数中的素数按才能够小到大的顺序输出(如果产生重复,只需要输出一个).
program number;
//By 灰天飞雁 htfy96@qq.com 转载请保留此行
{
TestData:
Input:
12
6 6 5 3 6 5 3 1 6 1 4 1
3
Output:
653
}
Var
  a:array[0..1000] of byte; //存储n个数字
  t:array[0..1000] of qword; //存储取出的n-l+1个数中是质数的
  n,i,l,top,j:longint;
  now,w,y:qword;
Function iszhi(P:qword):boolean;inline;//判断一个数是否为质数
var
  i:longint;
  begin
  for i:=2 to trunc(sqrt(p)) do
    if p mod i=0 then exit(false);
  exit(true);
end;
procedure check(P:qword);inline; //检查一个数是否为质数,若是则将其加入T中
  begin
  if iszhi(P) then
    begin
    inc(top);
    t[top]:=p;
  end;
end;
  begin
  top:=0; //top:T数组的栈顶
  readln(n);
  for i:=1 to n do
    read(a[i]);
  readln;
  readln(l);
  now:=0;//now 存储的是当前处理的长度为l的数
  w:=1; //w代表当前处理那一位的权值
  for i:=l downto 1 do
    begin
    now:=now+a[i]*w;
    w:=w*10;
  end;  //这一部分是计算第1~L位的那个数字的
  w:=w div 10; //最高位的权值在最后多乘了一个10
  check(now); //检查第一个数
  for i:=l+1 to n do //i代表了当前处理数的最后一位.当前处理数=前一个数除掉首位(mod w) *10 +当前这一位的数字(+a[i])
    begin
    now:=now mod w;
    now:=now*10+a[i];
    check(now);
  end;
  for i:=1 to top-1 do//数据不多就冒泡了……
    for j:=i+1 to top do
      if t[i]>t[j] then
        begin
        y:=t[i];
        t[i]:=t[j];
        t[j]:=y;
      end;
  for i:=1 to top do
    if (t[i]<>t[i-1]) or (i=1) then //去重
      writeln(t[i]);
  readln
end.