工具人Plus 2022-07-04 09:00 采纳率: 100%
浏览 32
已结题

如何实现数组的元素倒置

#include
using namespace std;

int main() {

int a = 0;
int arr[5] = { 1,2,3,4,5 };
int arr2[5];

for (int i = 4; i >0; i++)
{
    for (int j=0; j < 5; j++)
    {    
        arr2[j] = arr[i];
        cout << arr2[j] << endl;
    }
}

}
这个题目我想用数组给数组赋值的方法解决,但是行不通
这是我写的代码,结果报错为:
0x00007FF6E66B195A 处(位于 案例二:元素逆置.exe 中)引发的异常: 0xC0000005: 读取位置 0x00000063B0BE0000 时发生访问冲突。
请问怎么解决?

  • 写回答

3条回答 默认 最新

  • 快乐鹦鹉 2022-07-04 09:04
    关注

    首尾进行交换,不需要双循环

    #include <iostream>
    using namespace std;
    int main()
    {
        int a[5] = {1,2,3,4,5};
        int i=0,j=4,temp;
        while(i++<j--)
        {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        for(i=0;i<5;i++)
            cout<<a[i]<<" ";
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月21日
  • 已采纳回答 7月13日
  • 创建了问题 7月4日

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应