#include
using namespace std;
string a[30];
int main()
{
int n=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
for(int i=1;ifor(int j=i+1;j<=n;j++)
{
if(a[j]+a[i]>a[i]+a[j])
{
swap(a[j],a[i]);
}
}
}
}
for(int i=1;i<=n;i++)
{
cout<return 0;
}
修改如下,供参考:
#include <stdio.h>
#include <string.h>
int main()
{
char a[21][11], tmp[11];
int n, i, j;
scanf("%d", &n);
for (i = 0;i < n; i++)
scanf("%s", a[i]);
for (i = n - 1; i > 0; i--)
{
for (j = 0;j < i; j++)
{
if (strcmp(a[j],a[j+1]) > 0)
{
strcpy(tmp, a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],tmp);
}
}
}
for (i = 0;i < n; i++)
printf("%s", a[i]);
return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string a[30];
int main()
{
int n = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
for (int i = 1; i < n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (a[j] + a[i] > a[i] + a[j])
{
swap(a[j], a[i]);
}
}
}
}
for (int i = 1; i <= n; i++)
{
cout << a[i];
}
return 0;
}
该回答引用ChatGPT
参考这个
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(string s1, string s2) {
return s1 + s2 < s2 + s1;
}
int main() {
int n;
cin >> n;
vector<string> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
sort(nums.begin(), nums.end(), cmp);
string res;
for (int i = 0; i < n; i++) {
res += nums[i];
}
cout << res << endl;
return 0;
}