跳到正文

题解

[CSP-J 2025] 拼数

_H17_约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年11月1日
收录
2026年8月15日
更新
2026年11月13日
篇幅
约 1 分钟
获赞
6
题目
P14357

版本

  1. v2当前
  2. v1

题目分析

可以通过遍历字符串直接把数取下来,放到 vector 里。由于高位权重大,从大到小排序即可,这样一定组成的数最大。

可以用桶优化到 O(n)O(n),但我没写。

代码

C++
#include<bits/stdc++.h>
#define ALL(x) x.begin(),x.end()
using namespace std;
string s;
vector<int>val;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>s;
	for(auto ch:s)
		if(isdigit(ch))
			val.push_back(ch^48);
	sort(ALL(val),greater<int>());
	for(auto v:val)
		cout<<v;
	return 0;
}
原文
存档评论4 条抓取于