求解未校正序列 (4,3,2,1,5) 并将其更改为校正序列取决于我的建议
solve uncorrected sequence (4,3,2,1,5) and change it to corrected sequence depend on my suggestion
我尝试解决这个问题,并建议我安排升序以从小数升序到高数升序。
所以我用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(
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的递归函数按降序打印,然后按升序打印?
- 我的 Boost 正则表达式与任何内容都不匹配
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- 为什么regex_match与我的正则表达式不匹配
- regex_error。我的正则表达式有什么问题
- 我正试图在我的SFML Platformer中实现双跳,但它并没有按预期的方式工作
- 如何通过模板元编程确定我的 PC 是小端序还是大端序
- 我的 XML 标签正则表达式不起作用
- 为什么我的正则表达式会导致无限循环
- 觉得有必要改进我的正则表达式
- 我使用 x=a+x-x^2 并设置 g(x)=a+x-x^2 来找到正参数 a 的正平方根
- 我的最后一个正则表达式不起作用,但我无法弄清楚原因
- 为什么我的c++正则表达式匹配正确,但没有返回正确的值
- 为什么这个正则表达式在pcregrep中匹配,而在我的c++代码中不匹配?
- 我的正则表达式代码有什么问题?
- 为什么这个正则表达式会导致我的程序崩溃