奇怪了。。下午稀里糊涂写了半天总是不对,现在一把过了。。。
#include<cstdio>
#include<iostream>
#include <algorithm>
#include<cstring>
#include<sstream>
#include <cmath>
#include <set>
#include<map>
#include <vector>
#define ll long long
using namespace std;
const int N = 1e4+5;
bool cmp(int a,int b)
{
return a > b;
}
int main()
{
string str;
cin>>str;
int num[10]={0},arr[10];
int t=0,f=0;
for(int i = 0;i < str.size();i++)
{
num[str[i]-'0']=1;
}
for(int i = 0;i < 10;i++)
{
if(num[i] > 0)
arr[t++]=i;
}
sort(arr,arr+t,cmp);
printf("int[] arr = new int[]{");
cout<<arr[0];
for(int j = 1;j < t;j++)
{
cout<<","<<arr[j];
}
printf("};\nint[] index = new int[]{");
for(int i =0;i < str.size();i++)
{
for(int j = 0;j < t;j++)
{
if(str[i]-'0' == arr[j])
{
if(f==0)
{
cout<<j;
f = 1;
}
else
cout<<","<<j;
}
}
}
printf("};\n");
return 0;
}
本文分享了一段使用C++进行字符串处理的代码实例,通过将字符串中的字符转换为整数数组并进行排序,实现了对字符串中数字的重新排列。代码展示了如何读取字符串、转换字符类型、筛选有效数字、自定义比较函数以及使用标准模板库(STL)进行排序。
413

被折叠的 条评论
为什么被折叠?



