【入门2】分支结构

数的性质

题目描述
一些数字可能拥有以下的性质:
性质 1:是偶数;
性质 2:大于 4 且不大于 12。
小A 喜欢这两个性质同时成立的数字;Uim 喜欢这至少符合其中一种性质的数字;八尾勇喜欢刚好有符合其中一个性质的数字;正妹喜欢不符合这两个性质的数字。
输入格式
输入一个数字 x(0\le x \le 1000)x(0≤x≤1000)
输出格式
输出这 4 个人是否喜欢这个数字,如果喜欢则输出1,否则输出0,用空格分隔。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int[] temp = new int[4];
        if(a%2==0&&a > 4 && a <= 12)
            temp[0] = 1;
        if(a%2==0 ||( a > 4 && a <= 12))
            temp[1] = 1;
        if((a % 2 == 0 && !(a > 4 && a <= 12)) || (a%2 !=0 &&a > 4 && a <= 12))
            temp[2] = 1;
        if(a%2!=0 && ( a < 4 || a > 12))
            temp[3] = 1;
        for (int i = 0; i <temp.length ; i++) {
            System.out.print(temp[i]+" ");
        }
    }
}

闰年判断

输入一个年份(大于 1582 的整数 ),判断这一年是否是闰年,如果是输出 1,否则输出 0。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        if((a % 4 == 0 && a % 100 != 0)|| a % 400 == 0)
            System.out.println(1);
        else
            System.out.println(0);
    }
}

Apples

八尾勇喜欢吃苹果。她今天吃掉了 x(0\le x \le 100)x(0≤x≤100) 个苹果。英语课上学到了 apple 这个词语,想用它来造句。如果她吃了 1 个苹果,就输出 Today, I ate 1 apple.;如果她没有吃,那么就把 1 换成 0;如果她吃了不止一个苹果,别忘了 apple 这个单词后面要加上代表复数的 s。你能帮她完成这个句子吗?

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println("Today, I ate "+n+" apple"+(n > 1?"s.":"."));
    }
}

洛谷团队系统

在洛谷上使用团队系统非常方便的添加自己的题目。如果在自己的电脑上配置题目和测试数据,每题需要花费时间 5 分钟;而在洛谷团队中上传私有题目,每题只需要花费 3 分钟,但是上传题目之前还需要一次性花费 11 分钟创建与配置团队。现在要配置 n(n\le100)n(n≤100) 道题目,如果本地配置花费的总时间短,请输出 Local,否则输出 Luogu。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        if(n > 5)
            System.out.println("Luogu");
        else
            System.out.println("Local");
    }
}

肥胖问题

BMI 指数是国际上常用的衡量人体胖瘦程度的一个标准,其算法是 m/h^2(40\le m \le 120, 1.4 \le h \le 2.0)m/h
2
(40≤m≤120,1.4≤h≤2.0),其中 mm 是指体重(千克),hh 是指身高(米)。不同体型范围与判定结果如下:
小于 18.5:体重过轻,输出 Underweight;
大于等于 18.5 且小于 24:正常体重,输出 Normal;
大于等于 24:肥胖,不仅要输出 BMI 值(使用 cout 的默认精度),然后换行,还要输出 Overweight;
现在给出体重和身高数据,需要根据 BMI 指数判断体型状态并输出对应的判断。
对于非 C++ 语言,在输出时,请四舍五入保留六位有效数字输出,如果小数部分存在后缀 00,不要输出后缀 00。
请注意,保留六位有效数字不是保留六位小数。例如 114.5149114.5149 应该输出为 114.515114.515,9198.109198.10 应该输出为 9198.19198.1。

import java.math.BigInteger;
import java.util.Scanner;
import java.util.stream.StreamSupport;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double m = scanner.nextDouble();
        double h = scanner.nextDouble();
        double temp = m/(h*h);
        if(temp < 24){
            System.out.println(temp>18.5?"Normal":"Underweight");
        }else{
        //截取七个数字
        //看最后一位是入还是舍去
            long te = (int)(temp*Math.pow(10,7-String.valueOf((int)temp).length()));
            char a = String.valueOf(te).charAt(6);
            te = Long.parseLong(String.valueOf(te).substring(0,6));
            te = (a -'0') < 5?te:te+1;
            double sum = (te*1.0)/Math.pow(10,6-String.valueOf((int)temp).length());
            System.out.println(sum);
            System.out.println("Overweight");
        }
    }
}

三位数排序

三个数从小到大排序

import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[3];
       a[0] = scanner.nextInt();
       a[1] = scanner.nextInt();
       a[2] = scanner.nextInt();
        Arrays.sort(a);
       System.out.println(a[0]+" "+a[1]+" "+a[2]);
    }
}

月份天数

输入年份和月份,输出这一年的这一月有多少天。需要考虑闰年。

import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int year = scanner.nextInt();
        int month = scanner.nextInt();
        boolean y = (year%4 == 0 && year %100 !=0) || year%400 == 0;
        int temp = month == 2?(y == true?29:28):30;
        if(month == 1 || month == 3 || month == 5|| month == 7|| month == 8|| month == 10|| month == 12 )
            temp+=1;
        System.out.println(temp);
    }

}

不高兴的津津

题目描述
津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。但是津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。请你帮忙检查一下津津下周的日程安排,看看下周她会不会不高兴;如果会的话,哪天最不高兴。
输入格式
输入包括77行数据,分别表示周一到周日的日程安排。每行包括两个小于1010的非负整数,用空格隔开,分别表示津津在学校上课的时间和妈妈安排她上课的时间。
输出格式
一个数字。如果不会不高兴则输出00,如果会则输出最不高兴的是周几(用1, 2, 3, 4, 5, 6, 71,2,3,4,5,6,7分别表示周一,周二,周三,周四,周五,周六,周日)。如果有两天或两天以上不高兴的程度相当,则输出时间最靠前的一天。

import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int max = 0;
        int j = 0;
        for (int i = 1; i <= 7 ; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if(a + b > 8){
                if(a+b > max){
                    max = a+b;
                    j = i;
                }
            }
        }
        System.out.println(j);
    }

}

买铅笔

题目描述
P老师需要去商店买n支铅笔作为小朋友们参加NOIP的礼物。她发现商店一共有 3种包装的铅笔,不同包装内的铅笔数量有可能不同,价格也有可能不同。为了公平起 见,P老师决定只买同一种包装的铅笔。
商店不允许将铅笔的包装拆开,因此P老师可能需要购买超过nn支铅笔才够给小朋 友们发礼物。
现在P老师想知道,在商店每种包装的数量都足够的情况下,要买够至少n支铅笔最少需要花费多少钱。
输入格式
第一行包含一个正整数n,表示需要的铅笔数量。
接下来三行,每行用2个正整数描述一种包装的铅笔:其中第11\个整数表示这种 包装内铅笔的数量,第2个整数表示这种包装的价格。
保证所有的7个数都是不超过10000的正整数。
输出格式
11个整数,表示P老师最少需要花费的钱。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n  = scanner.nextInt();
         int min = Integer.MAX_VALUE;
        for (int i = 0; i < 3 ; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            min = Math.min(min,n%a ==0 ?((n/a)*b):((n/a+1)*b));
        }
        System.out.println(min);
    }
}

ISBN号码

题目描述
每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括99位数字、11位识别码和33位分隔符,其规定格式如x-xxx-xxxxx-x,其中符号-就是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如00代表英语;第一个分隔符-之后的三位数字代表出版社,例如670670代表维京出版社;第二个分隔符后的五位数字代表该书在该出版社的编号;最后一位为识别码。
识别码的计算方法如下:
首位数字乘以11加上次位数字乘以22……以此类推,用所得的结果\bmod 11mod11,所得的余数即为识别码,如果余数为1010,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码44是这样得到的:对067082162这99个数字,从左至右,分别乘以1,2,…,91,2,…,9再求和,即0×1+6×2+……+2×9=1580×1+6×2+……+2×9=158,然后取158 \bmod 11158mod11的结果44作为识别码。
你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出Right;如果错误,则输出你认为是正确的ISBN号码。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        //x-xxx-xxxxx-x
        int j = 1;
        int count = 0;
        int i= 0;
        while (j < 10){
            if (s.charAt(i) != '-'){
                count += ((s.charAt(i)-'0')*j);
                j++;
            }
            i++;
        }
        if (count%11 == 10 && s.charAt(s.length()-1) == 'X')
            System.out.println("Right");
        else if(s.charAt(s.length()-1)-'0' == (count%11) ){
            System.out.println("Right");
        }else if(count%11 == 10){
            System.out.println(s.substring(0,12)+"X");
        }else
            System.out.println(s.substring(0,12)+""+String.valueOf(count%11));
    }
}

小玉家的电费

题目描述
夏天到了,各家各户的用电量都增加了许多,相应的电费也交的更多了。小玉家今天收到了一份电费通知单。小玉看到上面写:据闽价电[2006]27号规定,月用电量在150千瓦时及以下部分按每千瓦时0.4463元执行,月用电量在151~400千瓦时的部分按每千瓦时0.4663元执行,月用电量在401千瓦时及以上部分按每千瓦时0.5663元执行;小玉想自己验证一下,电费通知单上应交电费的数目到底是否正确呢。请编写一个程序,已知用电总计,根据电价规定,计算出应交的电费应该是多少。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x  = scanner.nextInt();
        double sum = 0;
        double[] temp = {0.4463,0.4663,0.5663};
        if(x > 150){
            sum += temp[0]*150;
            if(x > 400)
                sum += temp[1]*250+(x-400)*temp[2];
            else
                sum += (x-150)*temp[1];
        }else{
            sum += x*temp[0];
        }
        System.out.println(String.format("%.1f",sum));
    }

}

小鱼的航程(改进版)

题目描述
有一只小鱼,它平日每天游泳 250 公里,周末休息(实行双休日),假设从周 x(1\le x \le 7)x(1≤x≤7) 开始算起,过了 n(n\le 10^6)n(n≤106 ) 天以后,小鱼一共累计游泳了多少公里呢?

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int n = scanner.nextInt();
        //只要过了七天就游泳250*5
        int count = (n/7)*250*5;
        int temp = n % 7;
        for (int i = 0; i < temp; i++){
            if( x + i != 6 && x+i != 7){
                count += 250;
            }
        }
        System.out.println(count);
    }
}

三角函数

输入一组勾股数 a,b c用分数格式输出其较小锐角的正弦值。(要求约分。)

import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[3];
        for (int i = 0; i < 3 ; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);
        //求n/m
        int te = gcd(a[2],a[0]);
        int m = a[2]/te;
        int n = a[0]/te;
        System.out.println(n+"/"+m);
    }
    static  int gcd(int n,int m){
        if(n < m){
            int t = n;
            n = m;
            m = t;
        }
        if(n % m == 0)
            return m;
        return gcd(m,n % m);
    }
}

淘淘摘苹果

陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 10个苹果。苹果成熟的时候,陶陶就会跑去摘苹果。陶陶有个 30 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试。
现在已知 10 个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹果的数目。假设她碰到苹果,苹果就会掉下来。

import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int apple_high[] = new int[10];
        for (int i = 0; i < 10 ; i++) {
            apple_high[i] = scanner.nextInt();
        }
        int high = scanner.nextInt()+30;
        int count = 0;
        for (int i = 0; i < 10 ; i++) {
            if(apple_high[i] <= high)count++;
        }
        System.out.println(count);
    }
}

三角形分类

给出三条线段 a,b,c的长度,均是不大于 10000 的整数。打算把这三条线段拼成一个三角形,它可以是什么三角形呢?
如果三条线段不能组成一个三角形,输出Not triangle;
如果是直角三角形,输出Right triangle;
如果是锐角三角形,输出Acute triangle;
如果是钝角三角形,输出Obtuse triangle;
如果是等腰三角形,输出Isosceles triangle;
如果是等边三角形,输出Equilateral triangle。
如果这个三角形符合以上多个条件,请分别输出,并用换行符隔开。

import javax.annotation.processing.SupportedSourceVersion;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[3];//边长
        for (int i = 0; i < 3; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);
        if(a[0] + a[1] > a[2] && a[1] - a[0] < a[2]){
            if(a[0] * a[0]+a[1] * a[1] == a[2]*a[2])
                System.out.println("Right triangle");
            if(a[0]*a[0]+a[1]*a[1]-a[2]*a[2] > 0) 
                System.out.println("Acute triangle");
            if(a[0]*a[0]+a[1]*a[1]-a[2]*a[2] < 0) 
                System.out.println("Obtuse triangle");
            if(a[0] == a[1]) 
                System.out.println("Isosceles triangle");
            if(a[0] == a[1] && a[1] == a[2]) 
                System.out.println("Equilateral triangle");
        }else{
            System.out.println("Not triangle");
        }
    }
}

ABC

You will be given three integers A, B and C. The numbers will not be given in that exact order, but we do know that A is less than B and B less than C. In order to make for a more pleasant viewing, we want to rearrange them in the given order.
The first line contains three positive integers A, B and C, not necessarily in that order. All three numbers will be less than or equal to 100. The second line contains three uppercase letters ‘A’, ‘B’ and ‘C’ (with no spaces between them) representing the desired order.

import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[3];//边长
        for (int i = 0; i < 3; i++) {
            a[i] = scanner.nextInt();
        }
        scanner.nextLine();
        Arrays.sort(a);
        char[] sort = scanner.nextLine().toCharArray();
        for (int i = 0; i < 3; i++) {
            System.out.print(a[sort[i]-'A']+" ");
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值