Stop the Hollyweb! No DRM in HTML5.

集合删数

描述:
一个集合有如下元素:1 是集合元素;若 P 是集合的元素,则 2 * P +1,4*P+5 也是集
合的元素,取出此集合中最小的 K 个元素,按从小到大的顺序组合成一个多位数,现要求
从中删除 M 个数位上的数字,使得剩下的数字最大,编程输出删除前和删除后的多位数字。
注:不存在所有数被删除的情况`
输入格式:
输入的仅一行,K,M 的值,K,M 均小于等于 30000。
输出格式:
输出为两行,第一行为删除前的数字,第二行为删除后的数字。
样例输入:
5 4
样例输出:
137915
95

program number;

(**
Prob=number
Date=2012/10/17
Author=HT
**)

Const
 maxn=4232447;

Var
 ct,i,n,k,m,j,q,ct9,maxi,max9:longint;
 v:array[0..maxn] of boolean;
 a,pre,next:array[0..30001] of longint;
 f:ansistring;
 s:string;

Procedure fopen;
  begin
  assign(input,'number.in');
  assign(output,'number.out');
  reset(input);
  rewrite(output);
end;

Procedure fclose;
  begin
  close(input);
  close(output);
end;

Procedure print;
var
 i:longint;
  begin
  writeln(f);
  fclose;
  halt;
end;

  begin
  fopen;

  fillchar(v,sizeof(v),false);
  v[1]:=true;

  for i:=1 to maxn do
    if v[i] then
      begin
      if 2*i+1<=maxn then v[2*i+1]:=true;
      if 4*i+5<=maxn then v[4*i+5]:=true;
    end;

  f:='';
  ct:=0;
  for i:=1 to maxn do
    if v[i] then
      begin
      inc(ct);
      a[ct]:=i;
    end;

  readln(k,m);
  for i:=1 to k do
    begin
    str(a[i],s);
    f:=f+s;
  end;

  writeln(f);

  if m=0 then print;

  ct9:=0;
  maxi:=0;
  max9:=0;
  for i:=1 to length(f) do
    if f[i]='9' then
      begin
      inc(ct9);
      if i-ct9<=m then
        begin
        maxi:=i;
        max9:=ct9;
      end;
    end;
  for i:=1 to max9 do write('9');

  delete(f,1,maxi);
  q:=m-maxi+max9;   ct:=0;
  f:=f+':';


    repeat
    dec(q);
    i:=1;
    while f[i]>=f[i+1] do inc(i);
    delete(f,i,1);
  until q=0;

  delete(f,length(f),1);

  writeln(f);

  fclose;

end.

 

posted on 2012-10-17 21:30  灰天飞雁  阅读(506)  评论(0编辑  收藏  举报

填写您的邮件地址,订阅我们的精彩内容:  点击这里给我发消息

添加到收藏夹