tne number which is made of the number of remaining is smallest

本文介绍了一种使用C++实现的算法,该算法通过比较字符串中相邻元素的大小,删除指定数量的较大值,最终输出处理后的字符串。此算法涉及字符串操作、循环和条件判断,适合初学者理解和实践。

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


void deleteDigite(int k, string str)
{
int len = str.length();
for(int i=0; i<k; i++) //there are k elements that need to be deleted
{

for(int j=0; j<len-1; j++) //the most times of cycles are len-1,if the second element is smaller than the previous,delete the first one, it is the great point
{
if(str[j]>str[j+1]) //if we meet the great point, we erase it
{
str.erase(j, 1);
len--;
break;
}
if(j==len-1) //if we arrive the end of string, but we did't find the element smaller than the previous one, the last one is a great point. erase it;
{
str.erase(j, 1);
}
}

}
cout << str;
}
int main()
{
string a = "1253879";
deleteDigite(2, a);
return 0;
}

转载于:https://www.cnblogs.com/1915884031A-qqcom/p/7569160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值