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

被折叠的 条评论
为什么被折叠?



