java 洛谷题单【入门2】分支结构

P2433        小学数学N合一

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        if (a == 1){
            System.out.println("I love Luogu!");
        }else if (a == 2){
            System.out.println("6 4");
        }else if (a == 3){
            System.out.println("3");
            System.out.println("12");
            System.out.println("2");
        }else if (a == 4){
            System.out.println("166.667");
        }else if (a == 5){
            System.out.println("15");
        }else if (a == 6){
            System.out.println(10.8167);
        }else if (a == 7){
            System.out.println("110");
            System.out.println("90");
            System.out.println("0");
        }else if (a == 8){
            
            System.out.println(31.4159);
            System.out.println(78.5398);
            System.out.println(523.599);
        }else if (a == 9){
            System.out.println(22);
        }else if (a == 10){
            System.out.println(9);
        }else if (a == 11){
            double x = 100.0 / 3;
            String t = String.format("%.4f", x);
            System.out.println(t);
        }else if (a == 12){
            System.out.println(13);
            System.out.println("R");
        }else if (a == 13){
            double x = 3.141593 * 1064 * 4 / 3;
            System.out.println((int)(Math.pow(x, 1.0/3)));
        }else if (a == 14){
            System.out.println(50);
        }
    }
}

P5709        Apples Prologue / 苹果和虫子

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(new BufferedInputStream(System.in));
		int m=sc.nextInt();
		int t=sc.nextInt();
		int s=sc.nextInt();
		int ans;
		if(m*t<=s) {
			ans=0;
		}else {
			double k=(double)s/t;
			ans=m-(int)Math.ceil(k);
		}
		System.out.println(ans);
		sc.close();
	}
}

P5710        数的性质

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();

        if (x < 0 || x > 1000){
            return;
        }
        int k = 0;
        if (x % 2 == 0) {
            k++;
        }
        if (x > 4 && x <= 12) {
            k++;
        }
        if (k == 2){
            System.out.print(1 + " ");
        }else System.out.print(0 + " ");

        if (k > 0){
            System.out.print(1);
        }else System.out.print(0);

        if (k == 1){
            System.out.print(" " + 1);
        }else System.out.print(" " +0);

        if (k == 0){
            System.out.print(" " + 1);
        }else System.out.print(" " + 0);

    }
}

P5711        闰年判断

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

    }
}

P5712        Apples

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();

        if (x == 1){
            System.out.println("Today, I ate 1 apple.");
        }else if (x > 1){
            System.out.println("Today, I ate " + x +  " apples.");
        }else if (x == 0){
            System.out.println("Today, I ate 0 apple.");
        }else System.exit(0);
    }
}

P5713        洛谷团队系统

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();

        if (x >= 6){
            System.out.println("Luogu");
        }else System.out.println("Local");
    }
}

P5714        肥胖问题


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int m = input.nextInt();
        double h = input.nextDouble();
        double x = m / (h * h);
        String a = String.format("%.4f",x);

        if (x < 18.5){
            System.out.println("Underweight");
        }else if (x >= 18.5 && x < 24){
            System.out.println("Normal");
        }else {
            System.out.println(a);
            System.out.println("Overweight");
        }
    }
}

P5715        三位数排序

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int tem;
        if (a > b){
            tem = a;
            a = b;
            b = tem;
        }
        if (a > c){
            tem = a;
            a = c;
            c = tem;
        }
        if (b > c){
            tem = b;
            b = c;
            c = tem;
        }
        System.out.println(a + " " +  b + " " + c);


    }
}

P5716        月份天数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int m = input.nextInt();

        int[] day;
        day = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (x % 4 == 0 && x % 100 != 0 || x % 400 == 0){
            day[2] = 29;
        }
        System.out.println(day[m]);
    }
}

P1085        不高兴的津津

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[] c = new int[7];
        int i;int max = 0; int count = 0;
        for (i = 0; i < 7; i++) {
            int a = input.nextInt();
            int b = input.nextInt();
            c[i] = a + b;
            if (c[i] > 8)count++;
            if (c[i] > max) max = c[i];
        }
        if (count == 0){
            System.out.println(0);
        }else {
            for (i = 0; i < 7; i++) {
                if (c[i] == max){
                    System.out.println(i + 1);
                    break;
                }
            }
        }


    }
}

P1909        买铅笔

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int num;int price;
        int[] a = new int[3];int min = 100000000;
        for (int i = 0; i < 3; i++) {
            num = input.nextInt();
            price = input.nextInt();
            int need;
            if (n % num == 0){
                need = n/num;
            }else need = n/num + 1;
            int count = need * price;
            a[i] = count;
            if (a[i] < min) min = a[i];
        }
        for (int i = 0; i < 3; i++) {
            if (a[i] == min){
                System.out.println(min);
                break;
            }
        }
    }
}

P5717        三角形分类

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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arr = new int[3];
        for (int i = 0; i < 3; i++) {
            arr[i] = input.nextInt();
        }
        Arrays.sort(arr);
        int max = arr[2];
        int mid = arr[1];
        int min = arr[0];
        if (max >= mid + min){
            System.out.println("Not triangle");
            return;
        }

        if (max * max == min * min + mid * mid) System.out.println("Right triangle");
        if (max * max < min * min + mid * mid) System.out.println("Acute triangle");
        if (max * max > min * min + mid * mid) System.out.println("Obtuse triangle");
        if (max == mid || mid == min) System.out.println("Isosceles triangle");
        if (max == min) System.out.println("Equilateral triangle");

    }
}

P1422        小玉家的电费

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        double a = 0.4463 * x;
        double b = 150 * 0.4463 + (x - 150) * 0.4663;
        double c = 150 * 0.4463 + 250 * 0.4663 + (x - 400) * 0.5663;
        String o = String.format("%.1f",a);
        String p = String.format("%.1f",b);
        String q = String.format("%.1f",c);

        if (x <= 150){
            System.out.println(o);
        }else if (x <= 400){
            System.out.println(p);
        }else {
            System.out.println(q);
        }

    }
}

P1424        小鱼的航程(改进版)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        int s = 0;
        for (int i = 0; i < y; i++) {
            if (x != 6 && x != 7) s += 250;
            if (x == 7) x = 1;
            else x++;
        }
        System.out.println(s);
    }
}

P1888        三角函数

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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arr = new int[3];
        for (int i = 0; i < 3; i++) {
            arr[i] = input.nextInt();
        }
        Arrays.sort(arr);
        int max = arr[2];
        int mid = arr[1];
        int min = arr[0];
        if (max * max == mid * mid + min * min){
            System.out.println(min/gcd(min, max) + "/" + max/gcd(min, max));
        }
    }
    public static int gcd(int p, int q){
        if(q == 0) return p;
        int r = p % q;
        return gcd(q, r);
    }
}

P1046        陶陶摘苹果

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a = new int[10];
        int count = 0;int i;
        for (i = 0; i < 10; i++) {
            a[i] = input.nextInt();
        }
        int h = input.nextInt();
        for (int k : a) {
            if (k <= h + 30) {
                count++;
            }
        }
        System.out.println(count);

    }
}

P4414        ABC

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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a = new int[3];
        for (int i = 0; i < 3; i++) {
            a[i] = input.nextInt();
        }
        Arrays.sort(a);
        String str = input.next();
        char c1 = str.charAt(0);
        char c2 = str.charAt(1);
        char c3 = str.charAt(2);
        System.out.println(a[c1 - 'A'] + " " + a[c2 - 'A'] + " " + a[c3 - 'A']);

    }
}

P1055        ISBN号码

import java.util.Scanner;

public class Main{
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		String s = input.nextLine();
		int sum=0;
		char c;
		sum=(s.charAt(0)-'0')*1+(s.charAt(2)-'0')*2+(s.charAt(3)-'0')*3+(s.charAt(4)-'0')*4+(s.charAt(6)-'0')*5+(s.charAt(7)-'0')*6+(s.charAt(8)-'0')*7+(s.charAt(9)-'0')*8+(s.charAt(10)-'0')*9;
		if(sum%11<10)
			c=(char)(sum%11+'0');
		else
			c='X';
		if(s.charAt(12)==c)
			System.out.println("Right");
		else {
			String r=s.substring(0, 12)+c;
			System.out.println(r);
		}
		
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeShen.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值