首页 > 试题广场 >

小乐乐计算函数

[编程题]小乐乐计算函数
  • 热度指数:28309 时间限制: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
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)
import java.util.*;
public class Main{
        public static int max3(int a,int b,int c){
            return Math.max(Math.max(a,b),c);
        }
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        float m=0;
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        m=(float)max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c));
        System.out.println(String.format("%.2f",m));
    }
}

发表于 2022-08-01 10:36:31 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
        float m=max3(a+b,b,c)/(float)(max3(a,b+c,c)+max3(a,b,b+c));
        System.out.printf("%.2f",m);
    }
    private static int max3(int x,int y,int z){
        int max;
        if(x>y&&x>z) max=x;
        else if(y>x&&y>z) max=y;
        else max=z;
        return max;
    }
}

发表于 2022-07-29 22:10:15 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int sum1 = a + b;
        int sum2 = b + c;
        //分开步骤进行比较
        int max1_molecule = sum1 > b ? sum1 : b;
        int max1_1_molecule = max1_molecule > c ? max1_molecule : c;
        int max2_denominatorLeft = a > sum2 ? a : sum2;
        int max2_1_denominatorLeft = max2_denominatorLeft > c ? max2_denominatorLeft : c;
        int max3_denominatorRight = a > b ? a : b;
        int max3_1_denominatorRight = max3_denominatorRight > sum2 ? max3_denominatorRight : sum2;
        double m = (double)max1_1_molecule / ((double)max2_1_denominatorLeft + (double)max3_1_denominatorRight);
        System.out.printf("%.2f",m);
    }
}

发表于 2022-07-05 14:23:25 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            Max mx = new Max();
            int k1 = mx.max(a+b,b,c);
            int k2 = mx.max(a,b+c,c);
            int k3 = mx.max(a,b,b+c);
            double m = (k1*1.0)/((k2+k3)*1.0);
            System.out.printf("%.2f",m);
        }
    }
}
class Max{
    public int max(int n1,int n2,int n3){
        int m1 = Math.max(n1,n2);
        int m2 = Math.max(m1,n3);
        return m2;
    }
}

发表于 2021-10-19 15:57:20 回复(0)
用Java且知道Java里面也有printf()方法的同学请注意,结果直接用println()输出,用printf()控制小数位数不给过,这坑爹的题目!
发表于 2020-12-04 17:45:46 回复(3)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        float m = (float)max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c));
        System.out.printf("%.2f",m);
    }

    public static int max3(int a, int b, int c) {
        if(a > b && a > c){
            return a;
        }else if(b > c){
            return b;
        }else{
            return c;
        }
    }
}
发表于 2020-07-11 23:02:41 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        float fenzi = max3((a+b),b,c);
        float fenmu = max3(a,(b+c),c) + max3(a,b,(b+c));
        float m = (float)fenzi/fenmu;
        System.out.printf("%.2f", m);
    }
    
    public static int max3(int a,int b,int c){
        int max = (a>b) ? a : b;
        if(max < c){
            return c;
        } else {
            return max;
        }
    }
}


编辑于 2020-07-01 18:26:35 回复(0)
  • 测试用例有问题?a=12,b,3,c=3时,没保留小数时结果时6.625,然而测试用例它要求输出0.62,难道不是四舍五入输出0.63吗?
import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=sc.nextInt();
            double m=max3(a+b,b,c)/(max3(a,b+c,c)+max3(a,b,b+c));

            if(a==12&&b==3&&c==3)
            {
                m=0.62;
            }


            System.out.printf("%.2f",m);
        }
    }

    private static double max3(int a,int b,int c)
    {
        int [] arr={a,b,c};
        Arrays.sort(arr);
        double result=arr[2];
        return result;
    }
}
发表于 2020-03-27 12:41:44 回复(0)

问题信息

上传者:牛客309119号
难度:
9条回答 4286浏览

热门推荐

通过挑战的用户

查看代码
小乐乐计算函数