程序代码如下:它的输出结果有问题!
#include<iostream.h>//冒泡排序
void main()
{
int num[10];
cout<<"输入10个数 用空格隔开"<<endl;
for(int i=0;i<10;i++)
{
cin>>num[i];
}
for( i=0;i<10;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
int temp;
for(int j=1;j<10;j++)
{
for(int a=0;a<10-j;a++)
if(num[a]>num[a+1])
{
int temp;
num[a]=temp;
num[a]=num[a+1];
num[a+1]=temp;
}
for(i=0;i<10;i++)
cout<<num[i]<<",";
cout<<endl;
}
}
请各位高手帮我修改下这个C++程序关于冒泡排序法
答案:3 悬赏:40
解决时间 2021-03-13 20:42
- 提问者网友:依靠
- 2021-03-13 16:10
最佳答案
- 二级知识专家网友:气场征服一切
- 2021-03-13 16:54
#include<iostream.h>//冒泡排序
void main()
{
int num[10];
cout<<"输入10个数 用空格隔开"<<endl;
for(int i=0;i<10;i++)
{
cin>>num[i];
}
for( i=0;i<10;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
for(int j=1;j<10;j++)
{
for(int a=0;a<10-j;a++)
{
if(num[a]>num[a+1])
{
int temp;
temp=num[a];
num[a]=num[a+1];
num[a+1]=temp;
}
}
}
for(i=0;i<10;i++)
cout<<num[i]<<",";
cout<<endl;
}
void main()
{
int num[10];
cout<<"输入10个数 用空格隔开"<<endl;
for(int i=0;i<10;i++)
{
cin>>num[i];
}
for( i=0;i<10;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
for(int j=1;j<10;j++)
{
for(int a=0;a<10-j;a++)
{
if(num[a]>num[a+1])
{
int temp;
temp=num[a];
num[a]=num[a+1];
num[a+1]=temp;
}
}
}
for(i=0;i<10;i++)
cout<<num[i]<<",";
cout<<endl;
}
全部回答
- 1楼网友:情窦初殇
- 2021-03-13 18:56
#include<iostream.h>//冒泡排序
void main()
{
int num[10];
cout<<"输入10个数 用空格隔开"<<endl;
for(int i=0;i<10;i++)
{
cin>>num[i];
}
for( i=0;i<10;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
int temp; //有点多余!!
for(int j=1;j<10;j++)
{
for(int a=0;a<10-j;a++)
if(num[a]>num[a+1])
{
int temp;
num[a]=temp; //temp 还没赋值吧
num[a]=num[a+1]; // 交换 也没写对
num[a+1]=temp;
}
for(i=0;i<10;i++)
cout<<num[i]<<",";
cout<<endl;
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@
错误的原因 给你标出来了
改的话就按楼上的改就行
- 2楼网友:陪我到地狱流浪
- 2021-03-13 18:05
template <class t>
void bubblesort(t *a,int n)
{
t tmp;
for (int i=1; i<n; i++)
for (int j=n-1; j>=i; j--)
{
if (a[j] < a[j-1])
{
tmp = a[j-1];
a[j-1] = a[j];
a[j] = tmp;
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯