Uva11491

本文介绍了一种使用贪心算法去除数字中最小数的方法。通过将数字转化为字符串存储,并利用结构体和排序算法,实现去除指定数量的最小数字,最终输出处理后的数字。代码采用C++实现,详细展示了算法步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:典型贪心法

就是每次都除去数字中最小的数字。,建议用字符串来存数字,好用

代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct number
{
	int i;
	char c;
	bool  operator < (const number b)const {//用c来存字符,用i来存索引
		return c < b.c;
	}
};
int main(void) {
	string n;
	cin >> n;
	int d;
	cin >> d;
	vector<number> v;
	for (int i = 0; i < n.length(); i++) {
		number s;
		s.c = n[i];
		s.i = i;
		v.push_back(s);
	}
	sort(v.begin(), v.end());//将前d个索引排除不输出
	for (int i = 0; i < n.length(); i++) {
		int j = 0;
		for (; j < d; j++) {
			if (i == v[j].i)
				break;
		}
		if (j == d)
			cout << n[i];
	}
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值