柱子竹 2024-04-16 15:03 采纳率: 0%
浏览 10

(标签-ios|关键词-stream)

只能通过60%用例,求帮助。
4、蚂蚁感冒:
https://www.lanqiao.cn/problems/221/learning/?page=1&first_category_id=1&tags=%E6%80%9D%E7%BB%B4

img

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int absolutevalue(int a) {
    return a >= 0 ? a : -a;
}    //求绝对值
bool cmp(int a,int b) {
    if (absolutevalue(a) < absolutevalue(b))
        return true;
    else
        return false;
}    
int main()
{
    int n;      //蚂蚁的总数
    int first;   //生病的蚂蚁
    int count = 1;//生病的蚂蚁数
    cin >> n;
    vector<int>ant,ant2;
    for (int i = 0; i < n; i++) {
        int antnum;
        cin >> antnum;
        ant.push_back(antnum);
    }
         first = ant[0];
    for (int i = 0; i < n; i++) {
        ant2.push_back(  absolutevalue(ant[i]));
       
    }
    sort(ant.begin(), ant.end(),cmp);
    /*for (int it : ant) {
        cout << it << " ";
    }*/
    auto it = find(ant.begin(), ant.end(), first);
    if (first == ant.back()) {
         for (auto iter = it; iter < ant.end();iter++) {
            if (*iter < 0) { count++; }
        }
    }
    if (first > 0) {
        for (auto iter = it; iter < ant.end();iter++) {
            if (*iter < 0) { count++; }
        }
        if (count > 1) {
            for (auto iter = ant.begin(); iter < it; iter++) {
                if (*iter > 0) { count++; }
            }
        }
        cout << count;
        return 0;
   }
    if (first < 0) {
        for (auto iter = ant.begin(); iter < it; iter++) {
            if (*iter > 0) { count++; }
        }
        if (count > 1) {
            for (auto iter = it; iter < ant.end(); iter++) {
                if (*iter < 0) { count++; }
            }
        }
        cout << count;
        return 0;
    }
    return 0;
}


  • 写回答

2条回答 默认 最新

  • GISer Liu 2024-04-16 16:15
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    根据你的描述,这个程序的目标是统计在一群蚂蚁中,离生病蚂蚁最近的健康蚂蚁的数量。程序的逻辑看起来大致正确,但在处理边界情况时可能存在一些问题。
    让我们来分析一下可能出现问题的地方:

    1. 当生病的蚂蚁是数组中的最大值或最小值时,似乎程序没有正确地处理这种情况。
    2. 某些情况下,程序可能无法正确计算健康蚂蚁的数量,特别是当生病的蚂蚁是正数或负数时。
      针对这些问题,我们需要做出一些修改:
    3. 在找到生病蚂蚁的位置后,需要分别计算左边和右边健康蚂蚁的数量。可以使用两个计数器来实现这一点。
    4. 考虑使用更简洁的逻辑来处理边界情况,而不是通过多个 if 语句来处理。
      下面是修改后的代码大纲:
      cppCopy code
      #include <iostream>
      #include <vector>
      #include <algorithm>
      using namespace std;
      int absolutevalue(int a) {
      return a >= 0 ? a : -a;
      }
      bool cmp(int a, int b) {
      return absolutevalue(a) < absolutevalue(b);
      }
      int main() {
      int n;  // 蚂蚁的总数
      int first;  // 生病的蚂蚁
      int count = 1;  // 生病的蚂蚁数
      cin >> n;
      vector<int> ant;
      for (int i = 0; i < n; i++) {
      int antnum;
      cin >> antnum;
      ant.push_back(antnum);
      }
      // 对蚂蚁按照距离生病蚂蚁的距离进行排序
      sort(ant.begin(), ant.end(), cmp);
      // 找到生病蚂蚁的位置
      auto it = find(ant.begin(), ant.end(), ant[0]);
      // 计算左侧健康蚂蚁的数量
      int leftCount = count(ant.begin(), it, [](int x){ return x > 0; });
      // 计算右侧健康蚂蚁的数量
      int rightCount = count(it, ant.end(), [](int x){ return x < 0; });
      // 输出总健康蚂蚁的数量
      cout << leftCount + rightCount << endl;
      return 0;
      }
      
      这个大纲涵盖了对问题的分析,并提供了一个修改后的代码框架。这个修改应该能够更准确地解决问题,并且提高程序的可读性和健壮性。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 创建了问题 4月16日

悬赏问题

  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题