题目如下
利用数组存储,简单的排序,从大到小输出,但是输出时注意末尾无空格
1.冒泡排序
int main()
{
int tmp,a[11];
//从大到小排序
for(int j=0;j<9;j++)
{
for(int i=0;i<9-j;i++)
{
if(a[i]<a[i+1])
{
//利用中间变量交换两数位置
tmp=a[i+1];
a[i+1]=a[i];
a[i]=tmp;
}
}
}
for(int i=0;i<10;i++)
{
if(i==9) cout<<a[i];
else cout<<a[i]<<" ";
}
return 0;
}
2.sort函数
这时候需要头文件
#include<iostream>
#include<algorithm>
int main()
{
int a[12]={0};//定义数组范围取大避免越界
sort(a,a+10);//注意sort排序为升序且左闭右开
for(int i=9;i>=0;i--)
{
if(i==0) cout<<a[i];
else cout<<a[i]<<" ";
}
return 0;
}
当然还有别的排序方法呀,这里就不给出代码了
注意格式的问题!!!