跳到正文

题解

P14357 [CSP-J 2025] 拼数 / number TJ

yxShaw约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P14357

版本

  1. v1当前

很简单的T1

题目大意:

给定一个字符串,求出该字符串的数字的最大值。

主要思路:

用if判断出是s[i]s[i]是否为数字,把字母筛掉,即:

C++
if(s[i] >= '0' && s[i] <= '9')

把字符或ASCLL码放到大根堆(优先队列)中

让STL自动帮我们排序 (大根堆排序默认大到小),随后前到后输出优先队列即可

核心代码

C++
for(int i = 0; i < s.length()/*或s.size()*/; i++)//注意,字符串从0开始遍历到n-1
    if(s[i] >= '0' && s[i] <= '9') pq.push(s[i]);//如果s[i]是数字,将它放到优先队列中。
while(!pq.empty())//队列/优先队列的输出方法
{
    cout << q.top();//输出队头
    q.pop();//队顶弹出
}

100pts code

ASCLL码:

C++
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define endl '\n'
using namespace std;

string s;
priority_queue<int> pq;//优先队列创建

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> s;int ls = s.length();
	for(int i = 0; i < ls; i++)
	{
		if(s[i] >= '0' && s[i] <= '9') pq.push(s[i]);//将数字入队
	}
	while(!pq.empty())
	{
		cout << (char)pq.top();//将队列int ASCLL码强转为char类型
		pq.pop();
	}
	return 0;
}

字符:

C++
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define endl '\n'
using namespace std;

string s;
priority_queue<char> pq;//优先队列创建

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> s;int ls = s.length();
	for(int i = 0; i < ls; i++)
	{
		if(s[i] >= '0' && s[i] <= '9') pq.push(s[i]);//将数字入队
	}
	while(!pq.empty())
	{
		cout << pq.top();//将队列int ASCLL码强转为char类型
		pq.pop();
	}
	return 0;
}
}
原文