马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
洛谷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);
- }
- }
- }
复制代码 P5714 【深基3.例7】肥胖问题
- 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);
- }
- }
复制代码 P5715 【深基3.例8】三位数排序
- 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);
- }
- }
复制代码 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();
- if(n == 0 || n == 1)
- System.out.println("Today, I ate "+n+" apple.");
- else
- System.out.println("Today, I ate "+n+" apples.");
- }
- }
复制代码 P1424 小鱼的航程(改进版)
- 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");
- }
- }
复制代码 P1888 三角函数
- 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");
- }
- }
- }
复制代码 P1046 [NOIP2005 普及组] 陶陶摘苹果
- 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]+" ");
- }
- }
- }
复制代码 P4414 [COCI2006-2007#2] ABC
- 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]);//方便输出
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |