小编Man*_*ese的帖子

为什么数组上的 std::binary_search 在 cmd 和 linux 终端中给出不同的结果?

我试图解决一个codeforces 问题,这要求我使用以下公式找到多边形的内角:

((n-2)*180)/n

; 其中“n”是多边形边数。我取了一个大小为 5 的数组来存储三角形(n=3)和正方形(n=4)的角度。编写以下代码以在数组上搜索角度(60 度):

#include<iostream>
#include<algorithm>

using namespace std;

int main(){
    int arr [5];
    
    for(int n=3; n<5; n++){
            arr[n] = ((n-2)*180)/n;
            cout << "arr["<<n<<"] = "<< arr[n];
            cout<<endl;
    }

    if(binary_search(arr, arr+5,60)){
               cout << "YES"<<"\n";
    }
    else{
               cout<< "NO"<<"\n";
    }   
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在命令提示符(cmd)上编译并运行它后,我得到了以下意想不到的结果:

arr[3] = 60 
arr[4] = 90
NO
Run Code Online (Sandbox Code Playgroud)

后来我在 Windows Subsystem for Linux 上尝试了相同的代码,并得到了以下正确的输出:

arr[3] = 60 
arr[4] = 90
YES
Run Code Online (Sandbox Code Playgroud)

为什么数组中 60 的 std::binary_search() 函数在 cmd 上返回 false?即使它存储在索引 3。为什么它在 linux 终端上工作? …

c++ arrays stl g++ windows-subsystem-for-linux

0
推荐指数
1
解决办法
62
查看次数

标签 统计

arrays ×1

c++ ×1

g++ ×1

stl ×1

windows-subsystem-for-linux ×1