求解未校正序列 (4,3,2,1,5) 并将其更改为校正序列取决于我的建议

solve uncorrected sequence (4,3,2,1,5) and change it to corrected sequence depend on my suggestion

本文关键字:正序 我的 取决于      更新时间:2023-10-16

我尝试解决这个问题,并建议我安排升序以从小数升序到高数升序。

所以我用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在一起 那么我的条件有什么问题

问候

算法是正确的。你弄乱了牙套。

#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::sort(