33,311
社区成员
发帖
与我相关
我的任务
分享
// huawei_test61.cpp : 定义控制台应用程序的入口点。
//
//将字符串中的数字从小到大排序
#include "stdafx.h"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char *test="a3vues4sdf2kjk9df0v";//排序的结果应该是a2vues3sdf4kjk9df0v
int len=strlen(test);
struct temp
{
char num;
int index;
};
temp*t=new temp[len];
int i=0;
int j=0;
while (i<len )
{
if (*(test+i)>='0'&&*(test+i)<='9') //将字符串中的数和下标取出来,放在结构体中
{
t[j].index=i;
t[j].num=*(test+i);
j++;
}
i++;
}
for (int m=j-1;m>0;m--) //对结构体数组的num排序,即 对数字进行排序,下标不变
{
for(int n=0;n<m;n++)
{
temp t1;
if (t[n].num>t[n+1].num)
{
t1.num=t[n].num;
t[n].num=t[n+1].num;
t[n+1].num=t1.num;
}
}
}
for (int m=0;m<j;m++) //排序之后,将t[m].num放回test[t[m].index],
{
test[t[m].index]=t[m].num;//这句总报错,写入位置时发生访问冲突
}
printf("%s",test);
return 0;
}