找最小数

标题:找最小数 | 时间限制:1秒 | 内存限制:32768K | 语言限制:不限
给一个正整数NUM1,计算出新正整数NUM2,NUM2为NUM1中移除N位数字后的结果,需要使得NUM2的值最小。


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string A;

void Df(string StrNum, int Lef)
{
	int n = StrNum.size();
	if (n == 0 || Lef == n)
	{
		return;
	}
	if (Lef == 0)
	{
		A += StrNum;
		return;
	}
	int d = 0;
	for (int i = 0; i < Lef + 1 && i < n; i++)
	{
		if (StrNum[i] < StrNum[d])
		{
			d = i;
		}
	}
	A += StrNum[d];
	if (d + 1 == n)
	{
		return;
	}
	Df(StrNum.substr(d + 1), Lef - d);
}

int main()
{
	string str;
	int n;
	cin >> str;
	cin >> n;
	A = "";
	Df(str, n);
	int t = 0;
	for (; t < A.size(); t++)
	{
		if (A[t] != '0')
		{
			break;
		}
	}
	if (t == A.size())
	{
		cout << 0;
	}
	else
	{
		for (; t < A.size(); t++)
		{
			cout << A[t];
		}
	}
	cout << endl;
	return 0;
}

def get_result(num1, num2):
    res_list, count, i = list() ,0, 0
    while i < len(num1):
        count = i
        while res_list and int(res_list[-1]) > int(num1[i]) and num2 != 0:
            res_list.pop()
            num2 -= 1
        res_list.append(num1[i])
        if num2 == 0:
            break
        i += 1
    res_list += num1[count + 1:]
    if num2 >0:
        res_list = res_list[: len(res_list) - num2]
    return res_list

while True:
    try:
        num1, num2 = list(input()), int(input())
        result = "".join(get_result(num1, num2)).lstrip("0")
        print(result if result else "0")
    except:
        break
//manfen
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author 刘骋
 * @description:
 * @date 2021/10/21 18:31
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        String row= sc.nextLine();
        LinkedList<Character> characters = new LinkedList<>();
        char[] chars = row.toCharArray();
        boolean flag=true;
        for (char value:chars){
            if ('a'<=value&&value<='z'||'A'<=value&&value<='Z'){
                characters.addLast(value);
            }else {
                flag=false;
                break;
            }

        }
        if (flag){
            int length= chars.length;
            while (true){
                LinkedList<Character> list = new LinkedList<>();
                int count=0;
                //不需要判断最后一个
                int size=characters.size();
                for(int i=0;i<size;i++){
                    Character test= characters.get(i);
                    if (i==size-1){
                        list.addLast(test);
                        continue;
                    }
                    if (test.equals(characters.get(i+1))){
                        count=1;
                        i++;
                        continue;
                    }
                    list.addLast(test);
                }
                characters=list;
                list=new LinkedList<Character>();
                if (count==0){
                    break;
                }
            }
            System.out.println(characters.size());
        }else {
            System.out.println("0");
        }
        
    }

}
//manfen

import sys
num = list(sys.stdin.readline().strip())
n = int(sys.stdin.readline().strip())
stack = []
m = len(num)
for i in range(m):
    while stack and int(stack[-1]) > int(num[i]):
        stack.pop()
        n -= 1
        if n == 0:
            break
    stack.append(num[i])
    if n == 0:
        break
stack += num[i + 1:]
if n > 0:
    stack = stack[:len(stack) - n]
res = "".join(stack).lstrip("0")
if res:
    print(res)
else:
    print("0")



全部评论

相关推荐

02-26 01:13
集美大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务