
C语言编程题 题目描述 使用冒泡排序法对数组元素进行排序,要求输出每一趟排序后的数组内容。数组大小 5
C语言编程题题目描述使用冒泡排序法对数组元素进行排序,要求输出每一趟排序后的数组内容。数组大小N<10,数组元素定为正整型。输入依次输入数组各个元素,各元素之间用空格隔开...
C语言编程题
题目描述
使用冒泡排序法对数组元素进行排序,要求输出每一趟排序后的数组内容。数组大小N<10,数组元素定为正整型。
输入
依次输入数组各个元素,各元素之间用空格隔开。
输出
用一行输出一趟排序后的结果。
样例输入
4 6 2 8 5 1
样例输出
4,2,6,5,1,8
2,4,5,1,6,8
2,4,1,5,6,8
2,1,4,5,6,8
1,2,4,5,6,8 展开
题目描述
使用冒泡排序法对数组元素进行排序,要求输出每一趟排序后的数组内容。数组大小N<10,数组元素定为正整型。
输入
依次输入数组各个元素,各元素之间用空格隔开。
输出
用一行输出一趟排序后的结果。
样例输入
4 6 2 8 5 1
样例输出
4,2,6,5,1,8
2,4,5,1,6,8
2,4,1,5,6,8
2,1,4,5,6,8
1,2,4,5,6,8 展开
展开全部
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
void sort(int arry[],int counts)//冒泡排序法
{
for(int i=0;i<counts;i++)
{
for(int j=0;j<counts-i-1;j++)
{
if(arry[j]>arry[j+1])//比较大小
{
int temp;
temp=arry[j];
arry[j]=arry[j+1];
arry[j+1]=temp;
}
}
for (int k=0;k<counts;k++)//输出
{
cout<<arry[k]<<" ";
}
cout<<'\n';
}
}
int main()
{
int arry[10];
char c;
int counts=0;
while((c=getchar())!='\n')//获取一行输入
{
if(c>='0'&&c<='9')
{
ungetc(c,stdin);//将获取的字符返回流
cin>>arry[counts++];
}
}
sort(arry,counts);
system("pause");
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
void sort(int arry[],int counts)//冒泡排序法
{
for(int i=0;i<counts;i++)
{
for(int j=0;j<counts-i-1;j++)
{
if(arry[j]>arry[j+1])//比较大小
{
int temp;
temp=arry[j];
arry[j]=arry[j+1];
arry[j+1]=temp;
}
}
for (int k=0;k<counts;k++)//输出
{
cout<<arry[k]<<" ";
}
cout<<'\n';
}
}
int main()
{
int arry[10];
char c;
int counts=0;
while((c=getchar())!='\n')//获取一行输入
{
if(c>='0'&&c<='9')
{
ungetc(c,stdin);//将获取的字符返回流
cin>>arry[counts++];
}
}
sort(arry,counts);
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |