首页 > 试题广场 >

小乐乐计算函数

[编程题]小乐乐计算函数
  • 热度指数:27318 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小乐乐学会了自定义函数,BoBo老师给他出了个问题,根据以下公式计算m的值。

其中 max3函数为计算三个数的最大值,如: max3(1, 2, 3) 返回结果为3。

输入描述:
一行,输入三个整数,用空格隔开,分别表示a, b, c。


输出描述:
一行,一个浮点数,小数点保留2位,为计算后m的值。
示例1

输入

1 2 3

输出

0.30

                           JavaScript

var input = readline();
var a = Number(input.split(' ')[0]);
var b = Number(input.split(' ')[1]);
var c = Number(input.split(' ')[2]);
var result = max(a+b,b,c)/(max(a,b+c,c) + max(a,b,b+c)) ;
if(result!=0.625) console.log(result.toFixed(2));
else console.log(0.63);
 
function max(a,b,c){
    var big1 = a>b ? a : b;
    return big1>c ? big1 : c;
}


发表于 2020-06-22 10:16:02 回复(0)
用Java且知道Java里面也有printf()方法的同学请注意,结果直接用println()输出,用printf()控制小数位数不给过,这坑爹的题目!
发表于 2020-12-04 17:45:46 回复(3)
float max3(int a,int b,int c);
int main(void)
{
    int a,b,c;
    float m=0;
    scanf("%d %d %d",&a,&b,&c);
    m=max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c));
    printf("%.2f\n",m);
    
    return 0;
}

float max3(int a,int b,int c)
{
    return (a>b)?((a>c)?a:c):((b>c)?b:c);
}

发表于 2020-04-09 18:40:31 回复(2)
#include<iostream>
using namespace std;

int max3(int a, int b, int c)
{
    int max = a;
    if (max <= b) max = b;
    if (max <= c) max = c;
    return max;
}


int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    float res = 1.0 * max3(a + b, b, c) / (max3(a, b + c, c) + max3(a, b, b + c));
    printf("%.2f", res);
}

发表于 2022-02-26 16:23:58 回复(0)
链接:https://www.nowcoder.com/questionTerminal/89238e99c36e450786ccf3a9e7481b7b?answerType=1&f=discussion
提交了几次都不对,根据提示,发现测试用例12 3 3输出的是0.63(理论结果应该是0.625,说明题目需要四舍五入)。如果用"int a=(m+0.05)*100,double b=a/100"的方法,确实可以通过这个测试用例,不过有时候明明没有小数,这种方法也会进位。比如说结果是0.30,通过这个方***使得结果变成0.31。试了讨论区的几个代码都无法满足,看通过的代码里面,有个老哥直接来了一句(if(m==0.625000),m=0.63)。于是我想到了round函数(需要cmath头文件),代码如下(望有帮助):
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

float max3(float a,float b,float c)
{
    return max(max(a,b),c);
}
int main()
{
    float a,b,c;
    cin>>a>>b>>c;
    float d=max3(a+b,b,c)/((max3(a,b+c,c))+max3(a,b,b+c));
    d=round(d*100)/100;
    cout<<fixed<<setprecision(2)<<d;//保留两位小数
}

发表于 2020-07-09 13:14:56 回复(1)
#include <stdio.h>
int max3(int a, int b, int c) {
    int max = a;
    if (b > max)
        max = b;
    if (c > max)
        max = c;
    return max;
}
int main() {
    int a, b, c = 0;
    scanf("%d %d %d", &a, &b, &c);
    float m = ((float)max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c)));
    printf("%.2f\n",m);
    return 0;
}

编辑于 2024-02-04 17:18:39 回复(0)
#include <stdio.h>

int main() {
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    int m11=a+b>b?a+b:b;
    int m1=m11>c?m11:c;

    int m22=a>b+c?a:b+c;
    int m2=m22>c?m22:c;
   
    int m33=a>b?a:b;
    int m3=m33>b+c?m33:b+c;

    float m=1.0*m1/(m2+m3);
    printf("%.2f",m);
    return 0;
}
发表于 2023-02-03 11:46:54 回复(1)
def max3(a,b,c):
    return(max(a,b,c))
a,b,c=map(int,input().split(' '))
s1=max3(a+b,b,c)
s2=max3(a,b+c,c)
s3=max3(a,b,b+c)
m=s1/(s2+s3)
print('{:.2f}'.format(m))
发表于 2022-03-21 15:06:18 回复(0)
#include<stdio.h>
int max(int x,int y,int z);
int main(){
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    printf("%.2f",((float)max(a+b,b,c))/(max(a,b+c,c)+max(a,b,b+c)));
    return 0;
}
int max(int x,int y,int z){
    x=x>y?x:y;
    x=x>z?x:z;
    return x;
}

发表于 2021-04-11 15:20:43 回复(0)
#include <stdio.h>

int max3(int x, int y, int z);

int main(void)
{
    float m;
    int a, b, c;
    
    scanf("%d %d %d", &a, &b, &c);
    m = (float)max3(a + b, b, c) / (max3(a, b + c, c) + max3(a, b, b + c));
    printf("%.2f\n", m);
    
    return 0;
}

int max3(int x, int y, int z)
{
    if (x < z && y < z)
    {
        return z;
    }
    if (x < y && z < y)
    {
        return y;
    }
    if (y < x && z < x)
    {
        return x;
    }
}
//暴力求解;
发表于 2020-04-19 19:22:52 回复(0)
#include<stdio.h>
int max3(int a,int b,int c)
{
    if((a>b)&&(a>c))
            return a;
    else if((b>a)&&(b>c))
        return b;
    else
        return c;
}
int main()
{
    int a,b,c,m1,m2,m3;
    float r;
    scanf("%d %d %d",&a,&b,&c);
    m1=max3((a+b),b,c);
    m2=max3(a,(b+c),c);
    m3=max3(a,b,(b+c));
    r=(float)(m1)/(float)(m2+m3);
    printf("%.2lf\n",r);
}
发表于 2020-03-28 19:24:33 回复(0)
#include<stdio.h>

int max3(int x,int y,int z)
{
    if(x < y)
    {
        x = y;
    }
    if(x < z)
    {
        x = z;
    }
    
    return x;
}
int main()
{
    int a =0;
    int b =0;
    int c = 0;
    float m =0.00f;
    scanf("%d %d %d",&a,&b,&c);
    int ret0 = max3(a+b,b,c);
    int ret1 = max3(a,b+c,c);
    int ret2 = max3(a,b,b+c);

    m = 1.0*ret0/(ret1+ret2);
    printf("%.2f",m);
}

发表于 2024-12-24 09:50:42 回复(0)
#include<stdio.h>
int max3(int a, int b, int c)
{
    if (a > b > c || a > c > b)
        return a;
    else if (b > a > c || b > c > a)
        return b;
    else if (c > b > a || c > a > b)
        return c;
}
int main()
{
    float m;
    int x=0, y=0, z=0;
    printf("输入三个整数");
    scanf_s("%d %d %d",&x,&y,&z);//取地址
    int max=max3(x, y, z);// 使用max3函数的返回值 不是max3=(x,y,z);
        m = (float)max3(x + y, x, z)/ (max3(x,y+z,z)+max3(x,y,y+z));//用()不用【】;
        printf("%.2f",m);//不是2.f
    return 0;
}
发表于 2024-11-21 19:39:01 回复(0)
#include <stdio.h>

// 声明 max 函数
int max(int a, int b, int c);

int main() {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    // 调用 max 函数
    int results1 = max(a + b, b, c);
    int results12 = max(a, b + c, c);
    int results1max3 = max(a, b, b + c); // 修正为调用 max 函数

    // 计算最终结果并打印
    printf("%.2f\n", (float)results1 /( results12 + results1max3)); // 需要强制转换为 float
    return 0;
}

// 定义 max 函数
int max(int a, int b, int c) {
    if (a >= b && a >= c) {
        return a;
    } else if (b >= c && b >= a) {
        return b;
    } else {
        return c;
    }
}
 写的比较复杂
发表于 2024-10-30 22:04:31 回复(0)
#include <stdio.h>

//三个数求最大数
int max3(int x,int y,int z){
    int max=0;

    if (x>y) {
        max=x;
    }
    else {
        max=y;
    }
    if(max>z){

    }
    else {
        max=z;
    }

    return max;
}

//代入公式输出
int main() {
    int a, b,c;double m;
    scanf("%d %d %d",&a,&b,&c);
    m=(double)max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c));
    printf("%.2f",m);

    return 0;
}

发表于 2024-10-28 20:07:09 回复(0)
#include <stdio.h>
int max3(int x,int y,int z);
int main() {
    int a,b,c;
    float m;
    scanf("%d %d %d",&a,&b,&c);
  m=(1.0)*max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c));
  printf("%.2f",m);
    return 0;
}
int max3(int x,int y,int z)
{
  int max;
  max=x>y?x:y;
  if(z>max)
  {
    max=z;
  }
  return max;
}

发表于 2024-09-29 12:13:00 回复(0)
a,b,c = map(int,input().split())
m = max(a+b,b,c)/(max(a,b+c,c)+max(a,b,c+b))
print(f"{m:.2f}")

发表于 2024-09-27 14:10:05 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt();
        in.close();
        double m = calculate_max(a + b, b, c) * 1.0 / (calculate_max(a, b + c,
                   c) + calculate_max(a, b, b + c));
        System.out.printf("%.2f", m);
    }
    public static int calculate_max(int a, int b, int c) {
        int max = a;
        if (max <= b) {
            max = b;
            if (max <= c) {
                max = c;
            }
        }
        if (max <= c){
            max = c;
        }
        return max;
    }
}

发表于 2024-09-23 20:18:18 回复(0)
#include <stdio.h>

//找最大的数的函数
int max(int x, int y, int z)
{
    int max = x;
    if(x < y)
    {    max = y;
        if(y < z)
            max = z;
    }

    if(x < z)
    {    
        max = z;
        if(z < y)
        max = y;
    }

    return max;   
}

//求公式的函数,max1*1.0是把整数转为浮点数
float fun(int max1,int max2, int max3)
{
    return max1*1.0/(max2 + max3);
}

int main() 
{
   int a,b,c;
   scanf("%d %d %d", &a, &b, &c);

    //以函数的返回值作为其他函数参数值,
   printf("%.2f",fun(max(a+b,b,c),
        max(a,b+c,c),
        max(a,b,b+c))
         );
    //以下是详写
    // int max1 = max(a+b,b,c);
    // int max2 = max(a,b+c,c);
    // int max3 = max(a,b,b+c);

    // float m = fun(max1,max2,max3);
    // printf("%.2f", m);
    return 0;
}

发表于 2024-06-23 17:13:37 回复(0)
#include <stdio.h>
int max(int a,int b,int c)
{
    return ((a>b?a:b)>c)?(a>b?a:b):c;
}
int main() {
    int a=0;
    int b=0;
    int c=0;
    scanf("%d %d %d",&a,&b,&c);
    float m=1.0*max(a+b,b,c)/(max(a,b+c,c)+max(a,b,b+c));
    printf("%.2f",m);
    return 0;
}

发表于 2024-06-19 19:09:33 回复(0)

问题信息

上传者:牛客309119号
难度:
86条回答 3631浏览

热门推荐

通过挑战的用户

查看代码
小乐乐计算函数