鱼C论坛

 找回密码
 立即注册
查看: 1210|回复: 3

[已解决]向足够大的有序数组a中,插入有序数组b,依然有序

[复制链接]
发表于 2020-8-31 10:23:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
报错我的函数必须有返回值
可是我的函数不是已经有返回值了吗?
#include<iostream>
using namespace std;

void insertArraySort(int* a, int length1, int* b, int length2)
{
        int length = length1 + length2;
        int indexOfA = length1 - 1;
        int indexOfB = length2 - 1;
        int indexOfNew = length - 1;
        if (a == nullptr || b == nullptr || length1 <= 0 || length2 <= 0)
                return;
        while (indexOfB >= 0)
        {
                if (b[indexOfB] <= a[indexOfA] && b[indexOfB] > a[indexOfA - 1])
                {
                        a[indexOfNew--] = b[indexOfB];
                        indexOfB--;
                }
                else
                {
                        a[indexOfNew--] = a[indexOfA--];
                }
               
        }
}

void main()
{  
        int a[9] = { 1, 2, 3, 5, 7 };
        int b[4] = { 2,4,7,9 };
        insertArraySort(a, 5, b, 4);
        cout << a << endl;
}
最佳答案
2020-8-31 10:27:13
main函数必须返回int类型的值:
  1. #include<iostream>
  2. using namespace std;

  3. void insertArraySort(int* a, int length1, int* b, int length2)
  4. {
  5.         int length = length1 + length2;
  6.         int indexOfA = length1 - 1;
  7.         int indexOfB = length2 - 1;
  8.         int indexOfNew = length - 1;
  9.         if (a == nullptr || b == nullptr || length1 <= 0 || length2 <= 0)
  10.                 return;
  11.         while (indexOfB >= 0)
  12.         {
  13.                 if (b[indexOfB] <= a[indexOfA] && b[indexOfB] > a[indexOfA - 1])
  14.                 {
  15.                         a[indexOfNew--] = b[indexOfB];
  16.                         indexOfB--;
  17.                 }
  18.                 else
  19.                 {
  20.                         a[indexOfNew--] = a[indexOfA--];
  21.                 }
  22.                
  23.         }
  24. }

  25. int main()
  26. {  
  27.         int a[9] = { 1, 2, 3, 5, 7 };
  28.         int b[4] = { 2,4,7,9 };
  29.         insertArraySort(a, 5, b, 4);
  30.         cout << a << endl;
  31.         return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-31 10:27:13 | 显示全部楼层    本楼为最佳答案   
main函数必须返回int类型的值:
  1. #include<iostream>
  2. using namespace std;

  3. void insertArraySort(int* a, int length1, int* b, int length2)
  4. {
  5.         int length = length1 + length2;
  6.         int indexOfA = length1 - 1;
  7.         int indexOfB = length2 - 1;
  8.         int indexOfNew = length - 1;
  9.         if (a == nullptr || b == nullptr || length1 <= 0 || length2 <= 0)
  10.                 return;
  11.         while (indexOfB >= 0)
  12.         {
  13.                 if (b[indexOfB] <= a[indexOfA] && b[indexOfB] > a[indexOfA - 1])
  14.                 {
  15.                         a[indexOfNew--] = b[indexOfB];
  16.                         indexOfB--;
  17.                 }
  18.                 else
  19.                 {
  20.                         a[indexOfNew--] = a[indexOfA--];
  21.                 }
  22.                
  23.         }
  24. }

  25. int main()
  26. {  
  27.         int a[9] = { 1, 2, 3, 5, 7 };
  28.         int b[4] = { 2,4,7,9 };
  29.         insertArraySort(a, 5, b, 4);
  30.         cout << a << endl;
  31.         return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-31 10:28:22 From FishC Mobile | 显示全部楼层
int main
才能return 0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-3 11:37:10 | 显示全部楼层
根据你使用的编译器决定的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-5 06:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表