繁体   English   中英

如何修复 HackerRank 的“错误:控制到达非空函数 [-Werror=return-type] 的末尾”?

[英]How to fix "error: control reaches end of non-void function [-Werror=return-type]" at HackerRank?

好的,已经提供了很多答案,但我似乎无法真正找到解决方案。

我们的任务是找到四个整数中最大的一个。

https://www.hackerrank.com/challenges/functions-in-c/problem

当我运行以下代码时:

#include <stdio.h>

/*
int max_of_four(int a, int b, int c, int d)
{
    if(a>b && a>c && a>d)
    return a;
    
    else if(b>a && b>c && b>d)
    return b;
    
    else if(c>a && c>b && c>d)
    return c;
    
    else
    return d;
}
*/

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    else if(b>c)
    {
        if(b>d)
        return b;
    }
    
    else if(c>d)
    return c;
    
    else
    return 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返回此错误:

Solution.c: In function ‘max_of_four’:
Solution.c:45:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
cc1: some warnings being treated as errors

但是上面的代码在 Dev-C++ 5.11 上运行时可以完成它的工作。

如果我们使用注释max_of_four的函数,并注释掉后一个,工作也完成了。

如果有人能让我知道如何调整max_of_four以使代码正常工作,我将不胜感激。

导致错误的原因是什么?

这是错误还是警告? 如果是这样,为什么警告被视为错误?

当函数不是 void 并且您使用if语句返回值时,您必须检查是否真的处理了每个案例。 在这种情况下,最后一个 else 无法确保这一点,因为您使用了没有else嵌套语句。

这是一个固定版本:

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    if(b>c) //<====== I removed the else here to fix the implicit
            //        else of the previous form that was not handled
    {
        if(b>d)
        return b;
    }
    
    if(c>d) //<===== Idem
    return c;
    
    else
    return d;
}

但是请注意,嵌套if in you 函数可以替换为and ,这会更紧凑:

int max_of_four(int a, int b, int c, int d)
{
    if (a>b && a>c && a>d) return a;
    else if(b>c && b>d) return b; // Here the else keys are kept even 
    else if(c>d) return c;        // if they are not strictly necessary
    else return d;                // because of the return
}

最后,编写以下代码也更具可重用性:

int max_of_two(int a, int b) { return a > b ? a : b; }

因为这样你就可以将它重用于任意数量的参数:

int max_of_three(int a, int b, int c) { 
    return max_of_two(max_of_two(a, b), c);
}
int max_of_four(int a, int b, int c, int d) { 
    return max_of_two(max_of_two(a, b), max_of_two(c, d));
}
//And so on

@dslr已经为上面提出的所有问题提供了最佳答案。

对于任何即将到来的读者, @Carcigenicate评论的内容可以从下面的代码中理解:

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
                return a;            
            else
                return d;
        }
        else if(c>d)
            return c;
        else
            return d;
    }
    else if(b>c)
    {
        if(b>d)
            return b;
        else
            return d;
    }
    else if(c>d)
    {
        return c;
    }
    else
        return d;
}

代码在 HackerRank 上运行流畅,没有出现任何错误。

这可能就是@Carcigenicate所说的“循环遍历数组”的意思:

int max_of_four(int a, int b, int c, int d)
{
    int i;
    int arr[4]={a,b,c,d};
    int max=arr[0];
    for(i=1;i<4;i++)
    {
        if(max<arr[i])
            max=arr[i];
    }
    return max;
}

谢谢大家。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM