• 那些年,我们一起做过的 Java 课后练习题(66 70)


    实例 66

    题目

    1 + 2 + 3 + ... + 100 的值。

    分析

    这里主要介绍两种方式:

    1. 循环遍历求和
    2. 公式求和:\(S = n(n+1)/2\)

    实现

    • 循环求和
    /**
     * Created with IntelliJ IDEA.
     *
     * @author : 村雨遥
     * @version : 1.0
     * @project : Java 编程实例
     * @package : PACKAGE_NAME
     * @className : Example66
     * @createTime : 2021/12/25 16:45
     * @email : 747731461@qq.com
     * @微信 : cunyu1024
     * @公众号 : 村雨遥
     * @网站 : https://cunyu1943.github.io
     * @description :
     */
    public class Example66 {
        public static void main(String[] args) {
            int sum = 0;
            int n = 100;
            for (int i = 11; i <= n; i++) {
                sum += i;
            }
            System.out.println("1 + 2 + ... + 100 = " + sum);
    
    
        }
    }
    
    
    • 公式求和
    /**
     * Created with IntelliJ IDEA.
     *
     * @author : 村雨遥
     * @version : 1.0
     * @project : Java 编程实例
     * @package : PACKAGE_NAME
     * @className : Example66
     * @createTime : 2021/12/25 16:45
     * @email : 747731461@qq.com
     * @微信 : cunyu1024
     * @公众号 : 村雨遥
     * @网站 : https://cunyu1943.github.io
     * @description :
     */
    public class Example66 {
        public static void main(String[] args) {
            int sum = 0;
            int n = 100;
            sum = n * (n + 1) / 2;
            System.out.println("1 + 2 + ... + 100 = " + sum);
        }
    }
    
    

    结果

    实例 67

    题目

    判断一个数 n 能否同时被 3 和 5 整除。

    分析

    由于 3 和 5 都是质数,要能同时被他们整除,则这个数一定能他们的最小公倍数。

    实现

    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @author : 村雨遥
     * @version : 1.0
     * @project : Java 编程实例
     * @package : PACKAGE_NAME
     * @className : Example67
     * @createTime : 2021/12/25 16:51
     * @email : 747731461@qq.com
     * @微信 : cunyu1024
     * @公众号 : 村雨遥
     * @网站 : https://cunyu1943.github.io
     * @description :
     */
    public class Example67 {
        public static void main(String[] args) {
            int num = 15;
            int n = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入 n");
            n = scanner.nextInt();
            if (n % num == 0) {
                System.out.println(n + "能同时被 3 和 5 整除。");
            } else {
                System.out.println(n + "不能同时被 3 和 5 整除。");
            }
        }
    }
    
    

    结果

    实例 68

    题目

    有一个函数:

    \[y=\begin{cases}x,& x < 1\\2x-1,&1<=x<10\\3x-11,&x>=10\end{cases} \]

    写程序,输入 x 的值,然后输出 y 对应的值。

    分析

    这里主要用条件判断语句,根据我们所输入的 x 调用不同的公式。

    实现

    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @author : 村雨遥
     * @version : 1.0
     * @project : Java 编程实例
     * @package : PACKAGE_NAME
     * @className : Example68
     * @createTime : 2021/12/25 17:02
     * @email : 747731461@qq.com
     * @微信 : cunyu1024
     * @公众号 : 村雨遥
     * @网站 : https://cunyu1943.github.io
     * @description :
     */
    public class Example68 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int x = 0;
            System.out.println("请输入 x");
            x = scanner.nextInt();
            if (x < 1) {
                System.out.println("y = " + x);
            } else if (x >= 1 && x < 10) {
                System.out.println("y = " + (2 * x - 1));
            } else {
                System.out.println("y = " + (x * 3 - 11));
            }
        }
    }
    

    结果

    实例 69

    题目

    给定一个不多于 5 位的正整数,要求:

    • 求出该数是几位数;
    • 分别输出每位数字;
    • 逆序输出各位数字,如原来为 123,应输出 321

    分析

    将该数转换为字符串,然后求其长度,然后正序输出字符串,再逆序输出字符串。

    实现

    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @author : 村雨遥
     * @version : 1.0
     * @project : Java 编程实例
     * @package : PACKAGE_NAME
     * @className : Example69
     * @createTime : 2021/12/25 17:07
     * @email : 747731461@qq.com
     * @微信 : cunyu1024
     * @公众号 : 村雨遥
     * @网站 : https://cunyu1943.github.io
     * @description :
     */
    public class Example69 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个不多于 5 位的正整数");
            int num = scanner.nextInt();
            String numStr = Integer.toString(num);
            System.out.println("该数为:" + numStr.length() + " 位数");
            for (int i = 0; i < numStr.length(); i++) {
                System.out.println(numStr.charAt(i));
            }
    
            for (int i = numStr.length() - 1; i >= 0; i--) {
                System.out.println(numStr.charAt(i));
            }
    
    
        }
    }
    

    结果

    实例 70

    题目

    找出一个二维数组中的鞍点,即该位置上的元素在该行最大,在该列上最大(也可能没有鞍点)。

    分析

    先找二位数组每一行的最大值,记录下该最大值的列数,再比较这个数在该列是否最大,若最大则存在。

    实现

    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @author : 村雨遥
     * @version : 1.0
     * @project : Java 编程实例
     * @package : PACKAGE_NAME
     * @className : Example70
     * @createTime : 2021/12/25 17:16
     * @email : 747731461@qq.com
     * @微信 : cunyu1024
     * @公众号 : 村雨遥
     * @网站 : https://cunyu1943.github.io
     * @description :
     */
    public class Example70 {
        public static void main(String[] args) {
            int[][] matrix = new int[5][5];
            Scanner scanner = new Scanner(System.in);
            //初始化数组
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    System.out.println("请输入 matrix[" + i + "][" + j + "]");
                    matrix[i][j] = scanner.nextInt();
                }
            }
    
            int maxY = 0;
            int maxX = 0;
            for (int i = 0; i < 5; i++) {
                maxX = matrix[i][0];
                boolean flag = true;
                // 求第 i 行最大值 maxX
                for (int j = 1; j < 5; j++) {
                    if (maxX < matrix[i][j]) {
                        maxX = matrix[i][j];
                        maxY = j;
                    }
                }
    
                for (int j = 1; j < 5; j++) {
                    if (maxX < matrix[j][maxY]) {
                        {
                            flag = false;
                            break;
                        }
                    }
                }
                if (flag) {
                    System.out.println("靶点位置:matxix[" + i + "][" + maxY + "]:" + maxX);
                }
            }
        }
    }
    

    结果

  • 相关阅读:
    Mosaic 前端微服务框架
    使用skipper 扩展fabio 的路由&&http proxy 功能
    Introducing Makisu: Uber’s Fast, Reliable Docker Image Builder for Apache Mesos and Kubernetes
    lua-resty-shell 多任务执行
    openresty 使用lua-resty-shell 执行shell 脚本
    ncm 让跨项目配置一致性简单的工具
    lapis 项目添加prometheus 监控集成grafana
    使用prometheus+ grafana+nginx-module-vts 模块监控openresty
    两天快速开发一个自己的微信小程序
    笔记本如何查看mac地址
  • 原文地址:https://www.cnblogs.com/cunyu1943/p/15731051.html
Copyright © 2020-2023  润新知