集合删数

描述:
一个集合有如下元素: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.

 

转载于:https://www.cnblogs.com/htfy/archive/2012/10/17/2728547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值