洛谷oj题单【入门2】分支结构-入门难度(Java)

[复制链接]
发表于 2023-2-6 05:33:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
洛谷oj题单【入门2】分支结构-入门难度(Java)

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

P5709 【深基2.习6】Apples Prologue / 苹果和虫子
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int m = sc.nextInt();
  6.         int t = sc.nextInt();
  7.         int s = sc.nextInt();
  8.         if (t == 0)
  9.             System.out.println(0);
  10.         else {
  11.             int apple = (int) Math.ceil(s / t);
  12.             if (m <= apple)
  13.                 System.out.println(0);
  14.             else
  15.                 if(s % t != 0)
  16.                     System.out.println(m - apple - 1);
  17.                 else
  18.                     System.out.println(m - apple);
  19.         }
  20.     }
  21. }
复制代码
P5714 【深基3.例7】肥胖问题
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int x = sc.nextInt();
  6.         //两种都符合
  7.         System.out.print(((x % 2 == 0) && ((x > 4) && (x <= 12))) ? 1 : 0);
  8.         System.out.printf(" ");
  9.         //至少符合一种
  10.         System.out.print(((x % 2 == 0) || ((x > 4) && (x <= 12))) ? 1 : 0);
  11.         System.out.printf(" ");
  12.         //只符合一种
  13.         System.out.print(((x % 2 == 0) ^ ((x > 4) && (x <= 12))) ? 1 : 0);
  14.         System.out.printf(" ");
  15.         //都不符合
  16.         System.out.print(((x % 2 != 0) && ((x <= 4) || (x > 12))) ? 1 : 0);
  17.     }
  18. }
复制代码
P5715 【深基3.例8】三位数排序
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int n = sc.nextInt();
  6.         if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0)
  7.             System.out.println(1);
  8.         else
  9.             System.out.println(0);
  10.     }
  11. }
复制代码
P1909 [NOIP2016 普及组] 买铅笔
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int n = sc.nextInt();
  6.         if(n == 0 || n == 1)
  7.             System.out.println("Today, I ate "+n+" apple.");
  8.         else
  9.             System.out.println("Today, I ate "+n+" apples.");
  10.     }
  11. }
复制代码
P1424 小鱼的航程(改进版)
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int n = sc.nextInt();
  6.         if(11 + 3 * n > 5 * n)
  7.             System.out.println("Local");
  8.         else System.out.println("Luogu");
  9.     }
  10. }
复制代码
P1888 三角函数
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         double m = sc.nextDouble();
  6.         double h = sc.nextDouble();
  7.         double BMI = m / (h * h);
  8.         if (BMI < 18.5)
  9.             System.out.println("Underweight");
  10.         else if (BMI >= 18.5 && BMI < 24)
  11.             System.out.println("Normal");
  12.         else {
  13.             if (BMI - (int) BMI == 0)
  14.                 System.out.printf("%2.0f\n", BMI);//用题目中给的m=120/h=1.4计算出整数部分不会超过两位,所以占位符取2
  15.             else if (BMI * 10 - (int) (BMI * 10) == 0)
  16.                 System.out.printf("%3.1f\n", BMI);//2位整数+1位小数点,所以占位符取3,保留小数点后1位,以下类推
  17.             else if (BMI * 100 - (int) (BMI * 100) == 0)
  18.                 System.out.printf("%4.2f\n", BMI);
  19.             else if (BMI * 1000 - (int) (BMI * 10000) == 0)
  20.                 System.out.printf("%5.3f\n", BMI);
  21.             else
  22.                 System.out.printf("%6.4f\n", BMI);
  23.             //用题目中给的m=120/h=1.4计算出整数部分不会超过两位
  24.             //所以6位有效数字最多就是2位整数+4位小数
  25.             System.out.println("Overweight");
  26.         }
  27.     }
  28. }
复制代码
P1046 [NOIP2005 普及组] 陶陶摘苹果
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         int[] str = new int[3];
  7.         for (int i = 0; i < 3; i++) {
  8.             str[i] = sc.nextInt();
  9.         }
  10.         int a = str[0], b = str[1], c = str[2];
  11.         Arrays.sort(str);
  12.         for (int i=0;i<3;i++)
  13.         {
  14.             System.out.print(str[i]+" ");
  15.         }
  16.     }
  17. }
复制代码
P4414 [COCI2006-2007#2] ABC
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int[] str = {0,31,28,31,30,31,30,31,31,30,31,30,31};//常规年份天数
  6.         int y = sc.nextInt();//年
  7.         int m = sc.nextInt();//月
  8.         if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0 )//判断是否为闰年,方便对2月进行判断
  9.             str[2] = 29;
  10.         System.out.printf("%d",str[m]);//方便输出
  11.     }
  12. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表