洛谷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);
else if (BMI * 10 - (int) (BMI * 10) == 0)
System.out.printf("%3.1f\n", BMI);
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);
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 )
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;
}
}
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();
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']);
}
}