啊哈磊_编程从这里起步

标题: 调用函数的问题,求大神告诉哪里错了。。谢谢。 [打印本页]

作者: 微风微凉丶    时间: 2018-3-10 20:32
标题: 调用函数的问题,求大神告诉哪里错了。。谢谢。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int max4=(int a,int b,int c,int d);
int a,b,c,d,max;
scanf("%d%d%d%d",&a,&b,&c,&d);
max=max4(a,b,c,d);
printf("max=%d\n",max);
return 0;
}
int max4=(int a,int b, int c,int d)
{
int max2=(int a,int b);
int m;
int m=max2(int a,int b);
int m=max2(int m,int c);
int m=max2(int m,int d);
return m;
}
int max2=(int a,int b)
{
if(a>=b)
  return a;
else
  return b;
}

作者: 创世菌    时间: 2018-3-10 20:32
标题: 优化
本帖最后由 创世菌 于 2018-3-10 20:57 编辑
创世菌 发表于 2018-3-10 20:43
一段符合你的思想的代码:

优化一下:
[mw_shl_code=c,true]#include<stdio.h>
#include<stdlib.h>
#define max2(A,B) ((A>B)?A:B)
#define max4(A,B,C,D) max2(max2(A,B),max2(C,D))
int main()
{
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    printf("max=%d\n",max4(a,b,c,d));
    system("pause");
    return 0;
}
[/mw_shl_code]
作者: 创世菌    时间: 2018-3-10 20:39
这能叫做代码?这是乱码好不好?
作者: 开天菌    时间: 2018-3-10 20:40
本帖最后由 开天菌 于 2018-3-10 20:42 编辑

你这定义函数还定义在主函数里啊......
作者: 开天菌    时间: 2018-3-10 20:43
还有,max就是一个函数,在math.h里.....
作者: 创世菌    时间: 2018-3-10 20:43
本帖最后由 创世菌 于 2018-3-10 20:58 编辑

一段符合你的思想的代码:
[mw_shl_code=c,true]#include<stdio.h>
#include<stdlib.h>
int max2(int a,int b)
{
        if(a>b)
                return a;
        else
                return b;
}
int max4(int a,int b,int c,int d)
{
        return max2(max2(a,b),max2(c,d));
}
int main()
{
        int a,b,c,d,max;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        max=max4(a,b,c,d);
        printf("max=%d\n",max);
        system("pause");
        return 0;
}
[/mw_shl_code]

作者: 开天菌    时间: 2018-3-10 20:44
你怎么定义这么多个叫m变量啊!
作者: zyx6680599    时间: 2018-3-10 20:44
返回数最好是在自定义函数里面 main最好返回零
作者: 开天菌    时间: 2018-3-10 20:45
好吧,我承认,我看不懂......



作者: 开天菌    时间: 2018-3-10 20:46
创世菌 发表于 2018-3-10 20:43
一段符合你的思想的代码:
[mw_shl_code=c,true]#include
#include

嘿!
他用的是C!!!
作者: 创世菌    时间: 2018-3-10 20:48
开天菌 发表于 2018-3-10 20:46
嘿!
他用的是C!!!

我是受过教训的:http://www.ahalei.com/forum.php? ... 1&fromuid=30773
还有什么话可说?
作者: 微风微凉丶    时间: 2018-3-10 20:57
#include<stdio.h>
#include<stdlib.h>
int main()
{
        int max4(int a,int b,int c,int d);
        int a,b,c,d,max;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        max=max4(a,b,c,d);
        printf("max=%d\n",max);
        return 0;
}
int max4(int a,int b,int c,int d)
{
        int max2(int a,int b);
        int m;
        m=max2(a,b);
        m=max2(m,c);
        m=max2(m,d);
        return m;
}
int max2(int a,int b)
{
        if(a>=b)
        return a;
        else
        return b;
}
       
作者: 创世菌    时间: 2018-3-10 21:10
本帖最后由 创世菌 于 2018-3-10 21:46 编辑

#include<stdio.h>
#include<stdlib.h>
int max4(int a,int b,int c,int d);
int max2(int a,int b);

int main()
{
        int a,b,c,d,max;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        max=max4(a,b,c,d);
        printf("max=%d\n",max);
        return 0;
}
int max4(int a,int b,int c,int d)
{
        int m;
        m=max2(a,b);
        m=max2(m,c);
        m=max2(m,d);
        return m;
}
int max2(int a,int b)
{
        if(a>=b)
        return a;
        else
        return b;
}




欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2