代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() {
int n;
scanf("%d", &n);
int a[21];
int i, j, k, q;
for (i = 0;i < n;i++) {
scanf("%d", &a[i]);
}
char s1[11], s2[11];
for (i = 0;i < n - 1;i++) {
sprintf(s1, "%d", a[i]);//
for (j = i + 1;j < n;j++) {
sprintf(s2, "%d", a[j]);
if (strlen(s1) < strlen(s2)) {//如果读取的两个数的位数不一样就用补齐到一样,把较短的数用它的最后一位一直补起来知道位数相等
for (k = strlen(s1);k < strlen(s2);k++) {
s1[k] = s1[strlen(s1) - 1];
}
}
else if (strlen(s1) > strlen(s2)) {
for (k = strlen(s2);k < strlen(s1);k++) {
s2[k] = s2[strlen(s2) - 1];
}
}
for (q = 0;q < strlen(s1);q++) {
if (s1[q] < s2[q]) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
break;
}
}
memset(s2, 0, sizeof(s2));//清空字符串
}
memset(s1, 0, sizeof(s1));//清空字符串
}
for (i = 0;i < n;i++) {
printf("%d", a[i]);
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() {
int n;
scanf("%d", &n);
int a[21];
int i, j, k, q;
for (i = 0;i < n;i++) {
scanf("%d", &a[i]);
}
char s1[11], s2[11];
for (i = 0;i < n - 1;i++) {
sprintf(s1, "%d", a[i]);//
for (j = i + 1;j < n;j++) {
sprintf(s2, "%d", a[j]);
if (strlen(s1) < strlen(s2)) {//如果读取的两个数的位数不一样就用补齐到一样,把较短的数用它的最后一位一直补起来知道位数相等
for (k = strlen(s1);k < strlen(s2);k++) {
s1[k] = s1[strlen(s1) - 1];
}
}
else if (strlen(s1) > strlen(s2)) {
for (k = strlen(s2);k < strlen(s1);k++) {
s2[k] = s2[strlen(s2) - 1];
}
}
for (q = 0;q < strlen(s1);q++) {
if (s1[q] < s2[q]) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
break;
}
}
memset(s2, 0, sizeof(s2));//清空字符串
}
memset(s1, 0, sizeof(s1));//清空字符串
}
for (i = 0;i < n;i++) {
printf("%d", a[i]);
}
return 0;
}