实例 66
题目
求 1 + 2 + 3 + ... + 100
的值。
剖析
这里次要介绍两种形式:
- 循环遍历求和
- 公式求和:$S = n(n+1)/2$
实现
- 循环求和
<code class="java">/** * Created with IntelliJ IDEA. * * @author : 村雨遥 * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example66 * @createTime : 2021/12/25 16:45 * @email : [email protected] * @微信 : 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); } }
- 公式求和
<code class="java">/** * Created with IntelliJ IDEA. * * @author : 村雨遥 * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example66 * @createTime : 2021/12/25 16:45 * @email : [email protected] * @微信 : 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 都是质数,要能同时被他们整除,则这个数肯定能他们的最小公倍数。
实现
<code class="java">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 : [email protected] * @微信 : 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
调用不同的公式。
实现
<code class="java">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 : [email protected] * @微信 : 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
剖析
将该数转换为字符串,而后求其长度,而后正序输入字符串,再逆序输入字符串。
实现
<code class="java">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 : [email protected] * @微信 : 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
题目
找出一个二维数组中的鞍点,即该地位上的元素在该行最大,在该列上最大(也可能没有鞍点)。
剖析
先找二位数组每一行的最大值,记录下该最大值的列数,再比拟这个数在该列是否最大,若最大则存在。
实现
<code class="java">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 : [email protected] * @微信 : 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); } } } }