洛谷oj题单【入门1】顺序结构-入门难度(Java)

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

B2002 Hello,World!

public class Main {
    public static void main(String[] args){
        System.out.println("Hello,World!");
    }

}

B2025 输出字符菱形

public class Main {
    public static void main(String[] args){
        System.out.println("  *  ");
        System.out.println(" *** ");
        System.out.println("*****");
        System.out.println(" *** ");
        System.out.println("  *  ");
    }


}

P1001 A+B Problem

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 = a+b;
        System.out.println(c);

    }


}

B2005 字符三角形

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        System.out.println("  "+s+"  ");
        System.out.println(" "+s+s+s+" ");
        System.out.println(s+s+s+s+s);

    }
}

B5703 【深基2.例5】苹果采购

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 apple = a * b;
        System.out.println(apple);

    }
}

P5704 【深基2.例6】字母转换

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
//        输入一个小写字母,输出其对应的大写字母。例如输入 q[回车] 时,会输出 Q。/
        Scanner sc = new Scanner(System.in);
        char a = sc.next().charAt(0);
        char b = (char) (a - 32);
        System.out.println(b);
    }
}

P5705 【深基2.例7】数字反转

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double num = sc.nextDouble();
        int n = (int)(num * 10);
        double temp = 0;
        while(n != 0){      //这里的n必须为int类型,若为double或float,n可无限趋近于0
            temp = n % 10 + temp * 10;
            n /= 10;
        }

        System.out.printf("%.3f",temp/1000);
    }
}

P5706 【深基2.例8】再分肥宅水

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float t = sc.nextFloat();
        int n = sc.nextInt();
        float a = t/n;
        int b = 2*n;
        System.out.printf("%.3f",a);
        System.out.println();
        System.out.println(b);

    }
}

P5708 【深基2.习2】三角形面积

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        double c = sc.nextDouble();
        double p =  (a + b + c) / 2.0;
        Math.sqrt(p*(p-a)*(p-b)*(p-c));
        System.out.printf("%.1f",Math.sqrt(p*(p-a)*(p-b)*(p-c)));
    }
}

B2029 大象喝水

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int r = sc.nextInt();
        int a = 20 * 1000;
        double b = 3.14 * r * r * h ;
        int n = (int)Math.ceil(a/b);
        System.out.println(n);
    }
}

P1425 小鱼的游泳时间

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 d = sc.nextInt();
        int x = c - a;
        int y = d - b;
        if(y < 0){
            x--;
            y = d + 60 - b;
        }
        System.out.print(x + " ");
        System.out.print(y);

    }
}

P1421 小玉买文具

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 x = a * 10 + b;
        int y = 1 * 10 + 9;
        int res = (int)Math.floor(x/y);
        System.out.println(res);

    }

}

P3954 [NOIP2017 普及组] 成绩

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 res = (int) (a * 0.2 + b * 0.3 + c * 0.5);
        System.out.println(res);
    }
}

你可能感兴趣的:(java,算法)