Description
定义两个重载函数,分别对整数数组和字符串数组用选择法从小到大排序,数组大小不超过10个元素,最后输出排序后的数据。Input
第一行的第一个整数表示数组元素个数n,后面n个整数为数组元素的值,数据之间用空格分开第二行的第一个整数表示数组元素个数n,后面n个字符串为数组元素的值,数据之间用空格分开,注意字符串里不包含空格
Output
第一行输出排序后的整数,第二行输出排序后的字符串,数据之间用空格分开(每行输出的最后一个数据后的空格需要单独处理)Sample Input
5 78 23 45 10 66
3 tracy mary alex
Sample Output
10 23 45 66 78
alex mary tracy
HINT
数组在函数间的传递
#include<iostream>
using namespace std;
void mysort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (a[i] > a[j])
swap(a[i], a[j]);
return;
}
void mysort(string str[], int n)
{
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (str[i]>str[j])
swap(str[i], str[j]);
}
int main()
{
int n;
cin >> n;
int a[110];
string str[110];
for (int i = 0; i < n; i++)
cin >> a[i];
mysort(a, n);
for (int i = 0; i < n-1; i++)
cout << a[i] << ' ';
cout << a[n - 1];
cout << endl;
cin >> n;
for (int i = 0; i < n; i++)
cin >> str[i];
mysort(str, n);
for (int i = 0; i < n-1; i++)
cout << str[i] << ' ';
cout << str[n - 1];
cout << endl;
return 0;
}