洛谷oj題單【入門1】順序結構-入門難度(Java) 來源:https://www.luogu.com.cn/training/100#problems B2002 Hello,World! public class Main { public static void main(String[] a ...
洛谷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);
}
}