CSUOJ 1270 Swap Digits
Description
Now we have a number, you can swap any two adjacent digits of it, but you can not swap more than K times. Then, what is the largest probable number that we can get after your swapping?
Input
There is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.
For each test case, there is an integer K (0 <= K < 106) in the first line, which has the same meaning as above. And the number is in the next line. It has at most 1000 digits, and will not start with 0.
There are at most 10 test cases that satisfy the number of digits is larger than 100.
Output
For each test case, you should print the largest probable number that we can get after your swapping.
Sample Input
3 2 1234 4 1234 1 4321
Sample Output
3124 4213 4321
Hint
暴力
#include<stdio.h> #include<string> #include<string.h> #include<algorithm> #include<iostream> typedef long long ll; using namespace std; int T, s, len; char ch[1010]; int main() { cin >> T; while (T--) { cin >> s >> ch; len = strlen(ch); for (int i = 0; i < len; i++) { if (s <= 0)break; char max = '0'; int key; for (int j = i + 1; j < len && j <= i + s; j++)//找到能移动的最大位数 { if (max < ch[j]) { max = ch[j]; key = j; } } if (max > ch[i]) { for (int j = key; j > i; j--) ch[j] = ch[j - 1]; ch[i] = max, s =s-( key - i); } } cout << ch << endl; } return 0; } /********************************************************************** Problem: 1270 User: leo6033 Language: C++ Result: AC Time:12 ms Memory:2024 kb **********************************************************************/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)