簡體   English   中英

為什么這個 C++ 程序對 17 13 3 5 不起作用

[英]Why does this C++ program doesn't work for 17 13 3 5

#include <iostream>
#include <cstdio>
using namespace std;


int max_of_four(int a, int b, int c, int d)
    {
         return( max((a,b),max(c,d)));
    }

int main()


{
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        int ans = max_of_four(a, b, c, d);
        printf("%d", ans);

        return 0;
    }

我在hackerRank上運行了這段代碼。 此代碼對於其他測試用例運行良好,除了:17 13 3 15

它輸出:15

我還嘗試為 17 13 運行 max(a,b) ,它理所當然地輸出 17。

請幫忙!

你忘了把max放在這里

return(max((a,b),max(c,d)));

它應該是return( max(max(a,b),max(c,d)));

順便說一句,你真的不需要return后的括號,因為它們只會使代碼更重並導致括號過多。

return( max((a,b),max(c,d)));

基本上評估為

return( max(b,max(c,d)));

可能你正在尋找

return  max( max(a,b), max(c,d) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM