Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 查找最大整数C++;_C++_Arrays - Fatal编程技术网

C++ 查找最大整数C++;

C++ 查找最大整数C++;,c++,arrays,C++,Arrays,给定一个由N个整数组成的数组A,返回最大整数K>0,这样数组A中就同时存在值K和-K(相反的数字)。如果没有这样的整数,函数应该返回0 例如: 给定A=[4,-4,-4,2],函数应该返回4 给定A=[-2,0,0,-3],函数应该返回0,因为没有这样的K>0 给定A=[-4],函数应返回0 下面是我已经实现的一些代码,但仍在试图找出获得数组中最大值的确切实现 #包括 整数解(向量&A){ 排序(A.begin(),A.end()); int first=0,last=A.size()-1

给定一个由N个整数组成的数组A,返回最大整数K>0,这样数组A中就同时存在值K和-K(相反的数字)。如果没有这样的整数,函数应该返回0

例如:

  • 给定A=[4,-4,-4,2],函数应该返回4

  • 给定A=[-2,0,0,-3],函数应该返回0,因为没有这样的K>0

  • 给定A=[-4],函数应返回0

  • 下面是我已经实现的一些代码,但仍在试图找出获得数组中最大值的确切实现

    #包括
    整数解(向量&A){
    排序(A.begin(),A.end());
    int first=0,last=A.size()-1;
    while(第一次<最后一次){
    如果(A[第一个]=-A[最后一个])
    返回[最后一个];
    如果(A[第一个]>-A[最后一个])
    最后-=1;
    其他的
    第一+=1;
    }
    返回0;
    }
    
    是错误的,您可能需要:

    if (A[first] == -A[last])
    

    (第一个是赋值,第二个是比较)

    我首先创建两个集合,一个是正数,另一个是负数的绝对值

    然后我会在两者之间做一个交集。结果中的最大值是您要查找的值(如果结果为空,则返回0)

    int求解(标准::向量常量和输入){
    std::设置位置;
    std::set neg;
    std::设置abs;
    std::copy_if(input.begin(),input.end(),std::inserter(pos,pos.end()),[](int i){return i>0;});
    std::copy_if(input.begin()、input.end()、std::inserter(neg、neg.end())、[](int i){return i<0;});
    std::transform(neg.begin(),neg.end(),std::inserter(abs,abs.end()),[](inti){return-i;});
    std::集合合并;
    std::set_交叉点(pos.begin()、pos.end()、abs.begin()、abs.end()、std::inserter(merged、merged.end());
    if(merged.empty())
    返回0;
    return*std::prev(merged.end(),1);
    }
    
    如果有一个
    transform\u,它能让您找到满足特定标准的项目,并在一个步骤中进行转换,那就太好了

    或者,ranges库可以将这一点(至少在语法上)简化很多

    值得一提的是,这里有一段简短的测试代码:

    int main(){
    向量输入{
    { { 4, -4, -4, 2}, 4 },
    { { -2,0, 0, -3}, 0 },
    { { -4}, 0 }
    };
    用于(自动[输入,结果]:输入){
    国际关系;
    如果((res=求解(输入))!=结果){
    
    std::cerr或者,使用
    std::sort
    std::nextant\u find
    ,您可以执行以下操作:

    int solve(std::vector<int>& v)
    {
        std::sort(v.begin(), v.end(), [](int lhs, int rhs){
            auto proj = [](int n){ return std::tuple(std::abs(n), 0 < n); };
    
            return proj(lhs) > proj(rhs);
        });
        auto it = std::adjacent_find(v.begin(), v.end(), [](int lhs, int rhs){ return lhs == -rhs; });
        return it != v.end() ? *it : 0;
    }
    
    int-solve(标准::向量&v)
    {
    排序(v.begin(),v.end(),[](int-lhs,int-rhs){
    autoproj=[](intn){返回std::tuple(std::abs(n),0项目(右侧);
    });
    autoit=std::相邻的查找(v.begin(),v.end(),[](int-lhs,int-rhs){return-lhs==-rhs;});
    返回它!=v.end()?*它:0;
    }
    

    如果(A[第一个]=-A[最后一个])
    正在执行赋值。
    ==
    用于检查相等性。打开警告-编译器确实希望帮助您解决类似问题。似乎如果向量/数组已排序,您只需查看
    A
    中的最后一个值,并验证它是否为非负值,对吗?无需循环。尝试按常量引用获取向量
    const vector&
    ,编译器会突然告诉您错误在哪里。@MichaelOrganger:
    [-5,-2,1,2,3,7]
    int solve(std::vector<int>& v)
    {
        std::sort(v.begin(), v.end(), [](int lhs, int rhs){
            auto proj = [](int n){ return std::tuple(std::abs(n), 0 < n); };
    
            return proj(lhs) > proj(rhs);
        });
        auto it = std::adjacent_find(v.begin(), v.end(), [](int lhs, int rhs){ return lhs == -rhs; });
        return it != v.end() ? *it : 0;
    }