C#贪心算法如何写..?
我找到了一个DELPHI版的代码,想把它写成C#版的..代码如下
program find_min_integer;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
N:string;
S,i:integer;
begin
write('N=');readln(N);
write('S=');readln(S);
while S>0 do
begin
i:=1;
while (i<length(N))and(n[i]<=n[i+1]) do inc(i);
delete(N,i,1);
dec(s);
end;
while (length(N)>1)and(N[1]='0') do delete(n,1,1);
write(N);
readln;
end.
C#的我尝试了写...可是编译不了....请教大大们....
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string N;
int S, i;
Console.Write("N="); N = Console.ReadLine();
Console.Write("S="); S = Console.Read();
while (S > 0)
{
i = 1;
while ((i < N.Length) && (N[i-1] <= N[i]))
{
i++;
}
N.Remove(i, 1);
S--;
}
while ((N.Length > 1) && (N[1] == '0'))
{
N.Remove(1, 1);
}
Console.Write(N);
}
}
}