【洛谷】【入门2】分支结构(Java8)

1.P2433 【深基1-2】小学数学 N 合一

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        double pi = 3.141593;
        Scanner sc = new Scanner(System.in);
        switch (sc.nextInt()) {
            case 1:
                System.out.println("I love Luogu!");
                break;
            case 2:
                System.out.print("6 4");
                break;
            case 3:
                System.out.println(3);
                System.out.println(12);
                System.out.println(2);
                break;
            case 4:
                double a = (double)500/3;
		        System.out.printf("%.3f", a);
                break;
            case 5:
                System.out.println((220+260)/(20+12));
                break;
            case 6:
                double b = Math.sqrt(6*6+9*9);
		        System.out.printf("%.4f",b);
                break;
            case 7:
                System.out.println(110);
                System.out.println(90);
                System.out.println(0);
                break;
            case 8:
			    double r=5;
			    System.out.printf("%.4f\n",2*pi*r);
			    System.out.printf("%.4f\n", pi*r*r);
			    System.out.printf("%.3f\n", 4.0/3.0*pi*r*r*r);
                break;
            case 9:
                System.out.println(22);
                break;
            case 10:
                System.out.println(9);
                break;
            case 11:
                double c = 1.0*100/3;
			    System.out.printf("%.4f", c);
                break;
            case 12:
                System.out.println((int)('M'-'A')+1);
			    System.out.println((char)('A'+18-1));
                break;
            case 13:
			    int r1=5,r2=10;
			    double v1 =4*pi*r1*r1*r1/3+4*pi*r2*r2*r2/3;
			    double v2 =Math.pow(v1, 1.0*1/3);
			    System.out.println((int)v2);
			    break;
            case 14:
                System.out.println(50);
                break;
        }
    }
}

2.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 eat = s/t + (s%t != 0? 1:0);
           System.out.println((m-eat) >= 0? (m-eat):0);
       }
    }
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int sum =0;
        if(r%2 == 0){
           sum++;
        }
        if(r>4 && r<=12){
           sum++;
        }
        if(sum==2){
           System.out.println("1 1 0 0");
        }else if(sum==1){
            System.out.println("0 1 1 0");
        }else{
            System.out.println("0 0 0 1");
        }
    }
}

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

import java.util.Scanner;

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

5.P5712 【深基3.例4】Apples

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        if (x <= 1) {
            System.out.printf("Today, I ate %d apple.",x);
        }else {
            System.out.printf("Today, I ate %d apples.",x);
        }
    }
}

6.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();
        int a = 5*n;
        int b = 11+3*n;
        if(a>b) {
            System.out.println("Luogu");
        }else {
            System.out.println("Local");
        }
    }
}

7.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(18.5<=BMI && BMI<24) {
            System.out.println("Normal");
        } else {
		System.out.printf("%.4f\n",BMI);
		System.out.println("Overweight");
	    }
	}
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] num = new int[3]; 
		for(int i=0;i<3;i++)
			num[i] = sc.nextInt();
		for(int i=0;i<num.length-1;i++) {
			for(int j=0;j<=num.length-1-j;j++) {
				if(num[j] > num[j+1]) {
					int temp = num[j];
					num[j] = num[j+1];
					num[j+1] = temp;
					}
				}
			}
		for(int i=0;i<3;i++)
		System.out.print(num[i]+" ");
    }
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        int month = sc.nextInt();
            switch (month){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    System.out.print("31");
                    return;
                case 4:
                case 6:
                case 9:
                case 11:
                    System.out.print("30");
                    return;
        }
        if (judge(year)==true){
            System.out.print("29");
        }
        else {
            System.out.print("28");
        }
    }
    public static boolean judge(int n){ 
        if(((n%4==0)&&n%100!=0)|| n%400==0) {
            return true;
        }
        else {
            return false;
        }
    }
}

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

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int sum, max = 0,day = 0;
		for(int i=0;i<7;i++) {
			int schoolIn = sc.nextInt();
			int schoolOut = sc.nextInt();
			sum = schoolIn + schoolOut;
			if(sum > max) { 
				max = sum;
				day = i + 1;
			}
		}
		if(max > 8) {
		    System.out.println(day);
		}else {
		    System.out.println(0);
		}
	}
}

11.P1909 [NOIP2016 普及组] 买铅笔

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int a;
        int b1,b2;
        int c1,c2;
        int d1,d2;
        Scanner sc = new Scanner(System.in);
        a=sc.nextInt();
        b1=sc.nextInt();b2=sc.nextInt();
        c1=sc.nextInt();c2=sc.nextInt();
        d1=sc.nextInt();d2=sc.nextInt();
        int m1,m2,m3;
        if(a%b1!=0){
            m1 = (a/b1+1)*b2;}
        else {
            m1 = (a/b1)*b2;
        }
        if(a%c1!=0){
             m2 = (a/c1+1)*c2;
        }else {
            m2 = a/c1*c2;
        }
        if(a%d1!=0){
            m3 = (a/d1+1)*d2;}
        else {
            m3 = (a/d1*d2);
        }
        if(m1<=m2&&m1<=m3) {
            System.out.print(m1);
        }else if(m2<=m1&&m2<=m3) {
            System.out.print(m2);
        }else if(m3<=m1&&m3<=m2) {
            System.out.print(m3);
        }
    }
}

12.P5717 【深基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 a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int[] array = {a,b,c};
        Arrays.sort(array);
        a = array[0];
        b = array[1];
        c = array[2];
        if (!((a + b > c) && ((c - a) < b) && ((b - a) < c) && ((c - b) < a))) {
            System.out.println("Not triangle");
            System.exit(0);
        }
        if ((a * a + b * b) > (c * c)) {
            System.out.println("Acute triangle");
        }
        if ((a * a + b * b) == (c * c)) {
            System.out.println("Right triangle");
        }
        if ((a * a + b * b) < (c * c)) {
            System.out.println("Obtuse triangle");
        }
        if (((a == b) || (b == c) ||(a == c))) {
            System.out.println("Isosceles triangle");
        }
        if ((a == b) &&(b == c)) {
            System.out.println("Equilateral triangle");
        }
    }
}

13.P1422 小玉家的电费

import java.util.Scanner;

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

14.P1424 小鱼的航程(改进版)

import java.util.Scanner;

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

15.P1888 三角函数

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		BigInteger[] arr = new BigInteger[3];
		for(int i=0;i<3;i++) {
		    arr[i] = sc.nextBigInteger();
		}
		Arrays.sort(arr);
		System.out.printf("%d/%d",arr[0].divide(arr[0].gcd(arr[2])),arr[2].divide(arr[0].gcd(arr[2])));
	}
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] AppleHeights = new int[10];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            int AppleHeight = sc.nextInt();
            AppleHeights[i] = AppleHeight;
        }
        int PeopleHeight = sc.nextInt() + 30;
        int j = 0;
        for (int i = 0; i < 10; i++) {
            if (AppleHeights[i] <= PeopleHeight) {
                j++;
            }
        }
        System.out.println(j);
    }
}

17.P4414 [COCI2006-2007#2] ABC

import java.util.Arrays;
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();
        String s = sc.next();
        int arr[] = {a,b,c};
        Arrays.sort(arr);
        if (s.equals("ABC")) {
            System.out.print(arr[0]+" "+arr[1]+" "+arr[2]);
        }
        else if(s.equals("ACB")) {
            System.out.print(arr[0]+" "+arr[2]+" "+arr[1]);
        }
        else if(s.equals("BAC")) {
            System.out.print(arr[1]+" "+arr[0]+" "+arr[2]);
        }
        else if(s.equals("BCA")) {
            System.out.print(arr[1]+" "+arr[2]+" "+arr[0]);
        }
        else if(s.equals("CAB")) {
            System.out.print(arr[2]+" "+arr[0]+" "+arr[1]);
        }
        else if(s.equals("CBA")) {
            System.out.print(arr[2]+" "+arr[1]+" "+arr[0]);
        }
    }
}

18.P1055 [NOIP2008 普及组] ISBN 号码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String rawCode = new Scanner(System.in).next();
        String code = rawCode.replace("-", "");
        int sum = 0, expect = code.charAt(9) - '0';
        expect = code.charAt(9) == 'X' ? 10 : expect;
        for (int i = 0; i < 9; i++){
            sum += (code.charAt(i) - '0') * (i + 1);
            sum %= 11;
        }
        if (expect == sum) {
            System.out.println("Right");
        }else {
            System.out.println(rawCode.substring(0, 12) + (sum == 10 ? "X" : sum));
        }
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值