洛谷oj題單【入門2】分支結構-入門難度(Java) 來源:https://www.luogu.com.cn/training/101#problems P5709 【深基2.習6】Apples Prologue / 蘋果和蟲子 import java.util.Scanner; public cl ...
洛谷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);//用題目中給的m=120/h=1.4計算出整數部分不會超過兩位,所以占位符取2
else if (BMI * 10 - (int) (BMI * 10) == 0)
System.out.printf("%3.1f\n", BMI);//2位整數+1位小數點,所以占位符取3,保留小數點後1位,以下類推
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);
//用題目中給的m=120/h=1.4計算出整數部分不會超過兩位
//所以6位有效數字最多就是2位整數+4位小數
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 )//判斷是否為閏年,方便對2月進行判斷
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;//i以0開頭,所以應加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();//從周x算起
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']);
}
}