我试图解决这个问题,并建议我安排升级换代,使一个正确的序列从小数上升到高。
所以我用c++编写了这个代码:-
include<iostream.h>
int main()
{
int ar[5] = {4,3,2,1,5};
int d;
for (int i=0; i<=4; i++)
cout<<ar[i]<<" ";
cout<<endl;
for (int x=0; x<=4; x++)
{
for(int y=x+1; y<=4; y++)
if(ar[x] > ar[y])
{
d=ar[x];
ar[x]=ar[y];
ar[y]=d;
for ( d=0; d<=4; d++)
{
cout<<ar[d]<<" ";
}
return 0;
}
}
}我得到了这个输出
Output
4 3 2 1 5
3 4 2 1 5 这是我第一次和cplus在一起,所以我的条件有什么问题?
问候
发布于 2018-06-17 23:12:48
该算法是正确的。你把牙套弄乱了。
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int ar[5] = {4,3,2,1,5};
int d;
for (int i=0; i<=4; i++)
cout<<ar[i]<<" ";
cout<<endl;
for (int x=0; x<=4; x++)
{
for(int y=x+1; y<=4; y++)
if(ar[x] > ar[y])
{
d=ar[x];
ar[x]=ar[y];
ar[y]=d;
}
}
for ( d=0; d<=4; d++)
{
cout<<ar[d]<<" ";
}
return 0;
}正确使用表格,以便更容易阅读和发现此类错误。
如果您的任务没有限制,请记住C++内置算法(例如,std:分类)。
https://stackoverflow.com/questions/50901046
复制相似问题