删数问题 delete.pas/c/cpp
输入一个高精度的正整数n(<=240位),去掉任意s个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的n和s,寻找一种方案,使得剩下的数最小。
【输入样例】
175438
4
【输出样例】
13
var
s:string;
i,n,j,k:longint;
begin
readln(s);
readln(n);
i:=1;
for j:= 1 to n do
for i:= 1 to length(s) do
if i=length(s) then
begin
delete(s,i,1);
break;
end else
if s[i]>s[i+1] then
begin
delete(s,i,1);
break;
end;
for i:=1 to length(s) do
begin
if s[i]<>'0' then begin k:=i; break; end;
end;
for i:=k to length(s) do
write(s[i]);
writeln;
end.
我的另一种:
var
s:string;
i,n,j,k:longint;
begin
readln(s);
readln(n);
s:=s;
for i:=1 to n do
begin
j:=1;
while (s[j]<=s[j+1])and(j<length(s)) do inc(j);
delete(s,j,1);
end;
for i:=1 to length(s) do
begin
if s[i]<>'0' then begin k:=i; break; end;
end;
for i:=k to length(s) do
write(s[i]);
writeln;
end.
别人的:
program delet;
var
a:array[1..240]of longint;
st:string;
i,n,s,j,k:longint;
begin
readln(st);
readln(s);
n:=length(st);
for i:= 1 to n do val(st[i],a[i]);
i:=1;
for j:= 1 to s do
while i<=n do
begin
if i=n then
begin
dec(n);
i:=1;
break;
end else
if a[i]>a[i+1] then
begin
for k:= i to n-1 do
a[k]:=a[k+1];
dec(n);
i:=1;
break;
end else inc(i);
end;
for i:= 1 to n do
begin
if a[i]<>0 then begin k:=i; break; end;
end;
for i:= k to n do write(a[i]);
writeln;
end.