使用C++将从控制台输入的一组字符串里面的数字提取出来,并降序排列。

如:510jhf尀`40]-853tlk;fj输出:85543100,谢谢了
2025-05-22 00:56:16
推荐回答(2个)
回答1:

#include 
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
string str;
cin >> str;
int len = str.size(),
i = 0,
j = 0;  // 第一个非数字位置
for (; i < len && j < len; )
{

while (j < len && isdigit(str[j]))
{
j++;
}
i = j;
while (i < len && !isdigit(str[i]))
{
i++;
}
if (i < len && j < len)
{
swap(str[j],str[i]);
i++;
j++;
}
}
sort(str.begin(),str.begin() + j,greater());
for (int i = 0; i < j; i++)
{
cout< }
//cout< return 0;
}

回答2:

// 5_19.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
#include
#include

int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "请输入字符: ";
std::string input;
std::cin >> input;

std::vector number;
if (!input.empty())
{
for (unsigned i = 0; i < input.size(); ++i)
{
if ((input[i] >= '0') && (input[i] <= '9'))
number.push_back(input[i]);
}
}
else
{
std::cout << "Error" << std::endl;
}

std::sort(number.begin(), number.end());

for (std::vector::iterator it = number.begin(); it != number.end(); ++it)
{
std::cout << *it << std::endl;
}
return 0;
}

写的比较仓促,细节上可能有点问题