C++ 定义一个有10个元素的一维数组count,从键盘输入8个整数,将其按从小到大的顺序排列,并将排列后的数组输出。
#include "stdio.h"
void main()
{
int a[10]={8,100,9,30,-10,32,89,76,91,12};
int i,j,temp,k;
for(i=1;i<10;i++)
{
for(j=9;j>=i;j--)
{
if(a[j]<a[j-1]){
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
//打印每次交换后的数据
printf("i=%d,j=%d:\t",i,j);
for(k=0;k<10;k++)
printf("%d\t",a[k]);
printf("\n");
}
}
for(i=0;i<10;i++)
printf("%d\t",a[i]);
}
void main()
{
int count[10],i,j,t;
for(i=0;i<8;i++)
cin<<count[i];
for(i=0;i<7;i++)
for(j=0;j<7-i;j++)
{
if(count[j] < count[j+1])
{
t = count[j];
count[j] = count[j+1];
count[j+1] = t;
}
}
for(i=0;i<8;i++)
cout<<count[i]<<" ";
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m