数组排序
1、定义交换两个整型数的函数swap
2、定义sort函数给数组排序,调用swap函数

Code
#include<iostream.h>
template<class T>
void swap(T &x,T &y)
{T temp;
temp=x;
x=y;
y=temp;
}
template<class p>
void sort(p *a,p b)
{
for(int i=0;i<10;i++)
{
for(int z=i;z<10;z++)
{
if(a[i]>a[z])
swap(a[i],a[z]);
}
}
cout<<"输出运算后的结果:"<<endl;
for(int j=0;j<10;j++)
cout<<a[j]<<" ";
}
void main()
{
int a[10];
cout<<"请输入10个数值:"<<endl;
for(int n=0;n<10;n++)
cin>>a[n];
sort(a,10);
}
相关文章:
-
2022-12-23
-
2022-12-23
-
2021-11-27
-
2022-12-23
-
2022-12-23
-
2022-01-04
-
2021-08-27
-
2021-06-13
猜你喜欢
-
2022-02-09
-
2022-12-23
-
2022-12-23
-
2022-02-09
-
2022-01-07
-
2021-06-04
-
2022-12-23
相关资源
-
下载
2022-12-02
-
下载
2021-06-06
-
下载
2022-12-29