鱼C论坛

 找回密码
 立即注册
查看: 422|回复: 1

[已解决]为什么编译不通过

[复制链接]
发表于 2023-12-20 17:55:58 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
这段c++代码为什么编译错误
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
using namespace std;

int L, M, N;//L为距离,N为岩石数目,M为可移去数
int A[50005];
int x;

bool check(int mid)
{
    int now = 1;
    int nex = 1;
    while(nex <= N + 1)
    {
        nex++;
        if (A[nex] - A[now] < mid)
        {
            nex++;
            x++;
        }
        else
        {
            now=nex;
        }
        if (x > M)
        {
            return 0;
        }
    }
    if (x > M)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main()
{
    cin>>L>>N>>M;
    A[N + 1] = L;
   
    for (int i = 1; i <= N; i++)
    {
        cin>>A[i];
    }
    int l=0,r=M;
   
    while (l < M)
    {
        mid=(l+r)/2;
        if (check(mid))
        {
            l = l + 1;
        }
        else
        {
            r = r - 1;
        }
    }
    cout<<mid<<endl;
   
    return 0;
}
最佳答案
2023-12-20 17:56:13
这段C++代码编译不通过的原因是缺少了必要的头文件。你需要包含iostream头文件来使用输入输出流,以及cmath来使用数学函数等。此外,代码中的while循环控制条件错误,逻辑上存在错误的括号配对,导致编译错误。

以下是修复后的代码:
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int L, M, N; // L为距离,N为岩石数目,M为可移去数
  5. int A;
  6. int x;
  7. bool check(int mid)
  8. {
  9.     int now = 1;
  10.     int nex = 1;
  11.     while (nex <= N)
  12.     {
  13.         if (mid >= x) // 此处是大于等于号
  14.         {
  15.             now++;
  16.             mid -= x;
  17.         }
  18.         else
  19.         {
  20.             nex++;
  21.             mid = L - x;
  22.         }
  23.         x = L - mid;
  24.         if (now > M)
  25.         {
  26.             return false;
  27.         }
  28.     }
  29.     if (x > M)
  30.     {
  31.         return false;
  32.     }
  33.     else
  34.     {
  35.         return true;
  36.     }
  37. }
  38. int main()
  39. {
  40.     cin >> L >> N >> M;
  41.     A = L;
  42.     for (int i = 1; i <= N; i++) // 此处是小于等于号
  43.     {
  44.         cin >> x;
  45.     }
  46.     int l = 0, r = M;
  47.     int mid; // mid需要提前定义
  48.     while (l < r) // 此处是小于号
  49.     {
  50.         mid = (l + r) / 2;
  51.         if (check(mid))
  52.         {
  53.             l = mid + 1; // 对应的左边界改为mid+1
  54.         }
  55.         else
  56.         {
  57.             r = mid; // 对应的右边界改为mid
  58.         }
  59.     }
  60.     cout << l - 1 << endl; // 输出结果
  61.     return 0;
  62. }
复制代码

希望对你有所帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-20 17:56:13 | 显示全部楼层    本楼为最佳答案   
这段C++代码编译不通过的原因是缺少了必要的头文件。你需要包含iostream头文件来使用输入输出流,以及cmath来使用数学函数等。此外,代码中的while循环控制条件错误,逻辑上存在错误的括号配对,导致编译错误。

以下是修复后的代码:
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int L, M, N; // L为距离,N为岩石数目,M为可移去数
  5. int A;
  6. int x;
  7. bool check(int mid)
  8. {
  9.     int now = 1;
  10.     int nex = 1;
  11.     while (nex <= N)
  12.     {
  13.         if (mid >= x) // 此处是大于等于号
  14.         {
  15.             now++;
  16.             mid -= x;
  17.         }
  18.         else
  19.         {
  20.             nex++;
  21.             mid = L - x;
  22.         }
  23.         x = L - mid;
  24.         if (now > M)
  25.         {
  26.             return false;
  27.         }
  28.     }
  29.     if (x > M)
  30.     {
  31.         return false;
  32.     }
  33.     else
  34.     {
  35.         return true;
  36.     }
  37. }
  38. int main()
  39. {
  40.     cin >> L >> N >> M;
  41.     A = L;
  42.     for (int i = 1; i <= N; i++) // 此处是小于等于号
  43.     {
  44.         cin >> x;
  45.     }
  46.     int l = 0, r = M;
  47.     int mid; // mid需要提前定义
  48.     while (l < r) // 此处是小于号
  49.     {
  50.         mid = (l + r) / 2;
  51.         if (check(mid))
  52.         {
  53.             l = mid + 1; // 对应的左边界改为mid+1
  54.         }
  55.         else
  56.         {
  57.             r = mid; // 对应的右边界改为mid
  58.         }
  59.     }
  60.     cout << l - 1 << endl; // 输出结果
  61.     return 0;
  62. }
复制代码

希望对你有所帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 13:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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