洛谷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 (int i=0;i<3;i++)
        {
            System.out.print(str[i]+" ");
        }
    }
}

P5716 [【深基3.例9】月份天数

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] str = {0,31,28,31,30,31,30,31,31,30,31,30,31};//常规年份天数
        int y = sc.nextInt();//年
        int m = sc.nextInt();//月
        if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0 )//判断是否为闰年,方便对2月进行判断
            str[2] = 29;
        System.out.printf("%d",str[m]);//方便输出
    }
}

P1085 [NOIP2004 普及组] 不高兴的津津

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a,b;
        int max = 0,res = 0;//分别为最大值,最终结果
        for (int i = 0; i < 7; i++) {
            a = sc.nextInt();
            b = sc.nextInt();
            if(a + b > 8 && a + b > max){
                max = a + b;
                res = i + 1;//i以0开头,所以应加1
            }
        }
        System.out.println(res);
    }
}

P1909 [NOIP2016 普及组] 买铅笔

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); //数量
        //三组数据
        int a1 = sc.nextInt();
        int a2 = sc.nextInt();
        int b1 = sc.nextInt();
        int b2 = sc.nextInt();
        int c1 = sc.nextInt();
        int c2 = sc.nextInt();
        //计算钱数
        int a = (int) (Math.ceil((double)n/(double)a1) * a2); //第一组
        int b = (int) (Math.ceil((double)n/(double)b1) * b2); //第二组
        int c = (int) (Math.ceil((double)n/(double)c1) * c2); //第三组
        //比较
        int min = a<b?a:b;
        min = min<c?min:c;
        //输出
        System.out.println(min);
    }
}

P1422 小玉家的电费

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double sum = 0;
        if(n < 150){
            sum = n * 0.4463;
        }else if(n >= 150 && n < 400){
            sum = 150 * 0.4463 + (n -150) * 0.4663;
        } else if (n >= 401) {
            sum = 150 * 0.4463 + 250 * 0.4663 + (n - 400) * 0.5663;
        }
        System.out.printf("%.1f",sum);
    }
}

P1424 小鱼的航程(改进版)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();//从周x算起
        int n = sc.nextInt();//天数
        int sum = 0;
        for (int i = 0; i < n; i++) {
            //在循环中进行星期的判断
            switch (x){
                case 1:case 2:case 3:case 4:case 5:sum += 250;//工作日接着游泳
                case 6:x++;continue;//周六休息
                case 7:x = 1;continue;//周日重置为周一,并且休息不游泳
            }
            x++;//进入下一天
        }
        System.out.println(sum);
    }
}

P1888 三角函数

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();
        int min = 0,max = 0;
        //求最小边
        min = a < b ? a : b;
        min = min < c ? min :c;
        //求斜边
        max = a > b ? a : b;
        max = max > c ? max :c;
        //辗转相除法求最大公因子
        int n = 0;
        for (int i = 1; i < min; i++) {
            if(max % i == 0 && min % i ==0){
                n = i;
            }
        }
        System.out.println(min/n+"/"+max/n );
    }
}

P1046 [NOIP2005 普及组] 陶陶摘苹果

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        int n = sc.nextInt();//总高度
        int num = 0;//个数
        for (int i = 0; i < arr.length; i++) {
            if(n+30 >= arr[i])
                num++;
        }
        System.out.println(num);
    }
}

P4414 [COCI2006-2007#2] ABC

import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] a = new int[3];
        a[0] = in.nextInt();
        a[1] = in.nextInt();
        a[2] = in.nextInt();
        Arrays.sort(a);//排序
        String str = in.next();
        char ch1 = str.charAt(0);//第一个字母
        char ch2 = str.charAt(1);//第二个字母
        char ch3 = str.charAt(2);//第三个字母
        System.out.println(a[ch1 - 'A'] + " " + a[ch2 - 'A'] + " " + a[ch3 - 'A']);
    }
}