1. 程式人生 > >刪數問題 山東理工oj2072(貪心)

刪數問題 山東理工oj2072(貪心)

刪數問題

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

 鍵盤輸入一個高精度的正整數n(≤100位),去掉其中任意s個數字後剩下的數字按照原來的左右次序組成一個新的正整數。程式設計對給定的n與s,尋找一種方案,使得剩下的數字組成的新數最小。

Input

  輸入有多組 每組包括原始數n,要去掉的數字數s;

Output

 輸出去掉s個數後最小的數

Sample Input

178543  4

Sample Output

13

老師佈置的演算法作業順便交一發

程式碼

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class Main {
	private static Scanner sc = new Scanner(System.in);
	public static void main(String[] args)throws FileNotFoundException{
		while(sc.hasNext())
		{
			String s = sc.next();
			int k = sc.nextInt();
			if(k >= s.length())
				System.out.println(0);
			else
			{
				int cnt = 0;
				for(int i = 0 ; i < s.length(); i ++)
					if(s.charAt(i) == '0')
						cnt++;
				if(s.length() - cnt <= k)
				{
					System.out.println(0);
					continue;
				}
				else 
				{
					while(k > 0)
					{
						if(s.charAt(0) > s.charAt(1))
						{
							k--;
							s = s.substring(1, s.length());
//							System.out.println(k + "    " + s);
							continue;
						}
						cnt = 1;
						for(int i = 0 ; i < s.length() - 1; i++)
						{
							if(s.charAt(i) <= s.charAt(i + 1))
								cnt++;
							else 
								break;
						}
						if(cnt != 1)
							cnt--;
						String gg = "" + s.charAt(cnt);
						s = s.replaceFirst(gg,"");
						k--;
					}
				}
				
				while(s.charAt(0) == '0')
					s = s.substring(1);
				System.out.println(s);
				
			}
		}
	}
}

佈置的作業

package 演算法作業;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class H2 {
	private static Scanner sc = new Scanner(System.in);
	public static void main(String[] args)throws FileNotFoundException{
		try 
		{
			sc = new Scanner(new FileInputStream("E:\\input.txt"));
			PrintStream ps = new PrintStream("E:\\output.txt");
			System.setOut(ps);//把建立的列印輸出流賦給系統。即系統下次向 ps輸出
			while(sc.hasNext())
			{
				String s = sc.next();
				int k = sc.nextInt();
				if(k >= s.length())
					System.out.println(0);
				else
				{
					int cnt = 0;
					for(int i = 0 ; i < s.length(); i ++)
						if(s.charAt(i) == '0')
							cnt++;
					if(s.length() - cnt <= k)
					{
						System.out.println(0);
						continue;
					}
					else 
					{
						while(k > 0)
						{
							if(s.charAt(0) > s.charAt(1))
							{
								k--;
								s = s.substring(1, s.length());
//								System.out.println(k + "    " + s);
								continue;
							}
							cnt = 1;
							for(int i = 0 ; i < s.length() - 1; i++)
							{
								if(s.charAt(i) <= s.charAt(i + 1))
									cnt++;
								else 
									break;
							}
							if(cnt != 1)
								cnt--;
							String gg = "" + s.charAt(cnt);
							s = s.replaceFirst(gg,"");
							k--;
						}
					}
					
					while(s.charAt(0) == '0')
						s = s.substring(1);
					System.out.println(s);
					
				}
			}	
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}