cf | Vadim‘s Collection
【代码】cf | Vadim's Collection。
·
题目:
代码:
无注释版:
#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
int main(){
int t;
cin>>t;
while(t--){
string s;
cin>>s;
mp.clear();
for(int i=0;i<s.size();i++){
mp[s[i]-'0']++;
}
for(int i=1;i<=10;i++){
int x=10-i;
for(int j=x;j<=9;j++){
if(mp[j]>0){
cout<<j;
mp[j]--;
break;
}
}
}
cout<<"\n";
}
}
有注释版:
#include<bits/stdc++.h> // 引入所有标准库
using namespace std;
map<int,int> mp; // 定义一个 map,记录每个数字(0-9)出现的次数
int main(){
int t;
cin >> t; // 读入测试用例数量
while(t--){
string s;
cin >> s; // 读入一串长度为10的数字字符串(电话号码)
mp.clear(); // 清空map,准备处理新的字符串
// 统计每个数字出现的次数
for(int i=0; i < s.size(); i++){
mp[s[i]-'0']++; // 将字符转成对应的数字,并计数
}
// 按照靓号规则输出新的最小靓号
for(int i=1; i <= 10; i++){
int x = 10 - i; // 当前位置需要的最小数字(例如第1位是9,第2位是8,...)
// 在 x ~ 9 范围内选最小的可用数字
for(int j = x; j <= 9; j++){
if(mp[j] > 0){ // 如果这个数字还有剩余
cout << j; // 输出这个数字
mp[j]--; // 使用掉一次
break; // 当前位确定,退出循环
}
}
}
cout << "\n"; // 每个电话号码输出后换行
}
}
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)