设为首页 加入收藏

TOP

洛谷oj题单【入门2】分支结构-入门难度(Java)(一)
2023-07-26 08:16:15 】 浏览:76
Tags:洛谷 题单 入门 支结构 Java

洛谷oj题单【入门2】分支结构-入门难度(Java)

来源:https://www.luogu.com.cn/training/101#problems

P5709 【深基2.习6】Apples Prologue / 苹果和虫子

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int t = sc.nextInt();
        int s = sc.nextInt();
        if (t == 0)
            System.out.println(0);
        else {
            int apple = (int) Math.ceil(s / t);
            if (m <= apple)
                System.out.println(0);
            else
                if(s % t != 0)
                    System.out.println(m - apple - 1);
                else
                    System.out.println(m - apple);

        }
    }
}

P5710 【深基3.例2】数的性质

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        //两种都符合
        System.out.print(((x % 2 == 0) && ((x > 4) && (x <= 12))) ? 1 : 0);
        System.out.printf(" ");
        //至少符合一种
        System.out.print(((x % 2 == 0) || ((x > 4) && (x <= 12))) ? 1 : 0);
        System.out.printf(" ");
        //只符合一种
        System.out.print(((x % 2 == 0) ^ ((x > 4) && (x <= 12))) ? 1 : 0);
        System.out.printf(" ");
        //都不符合
        System.out.print(((x % 2 != 0) && ((x <= 4) || (x > 12))) ? 1 : 0);

    }
}

P5711 【深基3.例3】闰年判断

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0)
            System.out.println(1);
        else
            System.out.println(0);
    }
}

P5712[【深基3.例4】Apples

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n == 0 || n == 1)
            System.out.println("Today, I ate "+n+" apple.");
        else
            System.out.println("Today, I ate "+n+" apples.");

    }
}


P5713 【深基3.例5】洛谷团队系统

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(11 + 3 * n > 5 * n)
            System.out.println("Local");
        else System.out.println("Luogu");

    }
}

P5714 【深基3.例7】肥胖问题

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double m = sc.nextDouble();
        double h = sc.nextDouble();
        double BMI = m / (h * h);
        if (BMI < 18.5)
            System.out.println("Underweight");
        else if (BMI >= 18.5 && BMI < 24)
            System.out.println("Normal");
        else {
            if (BMI - (int) BMI == 0)
                System.out.printf("%2.0f\n", BMI);//用题目中给的m=120/h=1.4计算出整数部分不会超过两位,所以占位符取2
            else if (BMI * 10 - (int) (BMI * 10) == 0)
                System.out.printf("%3.1f\n", BMI);//2位整数+1位小数点,所以占位符取3,保留小数点后1位,以下类推
            else if (BMI * 100 - (int) (BMI * 100) == 0)
                System.out.printf("%4.2f\n", BMI);
            else if (BMI * 1000 - (int) (BMI * 10000) == 0)
                System.out.printf("%5.3f\n", BMI);
            else
                System.out.printf("%6.4f\n", BMI);
            //用题目中给的m=120/h=1.4计算出整数部分不会超过两位
            //所以6位有效数字最多就是2位整数+4位小数
            System.out.println("Overweight");
        }
    }
}

P5715 【深基3.例8】三位数排序

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] str = new int[3];
        for (int i = 0; i < 3; i++) {
            str[i] = sc.nextInt();
        }
        int a = str[0], b = str[1], c = str[2];
        Arrays.sort(str);
        for (
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇QA 不讲武德!线上 1 亿+ 数据乱.. 下一篇力扣 977.有序数组的平方(Java)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目