This article has been synchronized to: Murakami Haruka
Example 1
topic
Classical problem: There is a pair of rabbits. From the third month after birth, a pair of rabbits is born every month. After the baby rabbit grows to the third month, another pair of rabbits is born every month. If the rabbits are not dead, ask each one. How many pairs of rabbits are there in a month? (outputs the number of rabbit pairs for the 10th month)
analyze
In fact, this is a typical Fibonacci sequence problem:
1st month: 1 pair of rabbits
Month 2: 1 pair of rabbits
3rd month: 2 pairs of rabbits
Month 4: 3 pairs of rabbits
...
That is, starting from the 3rd month, the rabbit logarithm of the current month is the sum of the rabbit logarithms of the previous two months of the current month.
accomplish
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @date : 2021/6/1 13:13
* @project : Java 编程实例
* @package : PACKAGE_NAME
* @className : Example1
* @description :
*/
public class Example1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int month = 0;
System.out.println("输入月份");
month = scanner.nextInt();
int former = 1;
int latter = 1;
for (int i = 1; i <= month; i++) {
// 第 1、2 个月的情况
if (i < 3) {
latter = 1;
} else {
int tmp = latter;
latter += former;
former = tmp;
}
System.out.format("第 %d 个月的兔子对数:%d\n", i, latter);
}
}
}
result
Example 2
topic
Determine the number of prime numbers between 100 - 200 and output.
analyze
To judge whether a number is a prime number, just divide the number by 2 ~ sqrt(这个数)
, once it is divided, it means that the number is not a prime number, otherwise it is a prime number.
accomplish
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @date : 2021/6/1 13:30
* @project : Java 编程实例
* @package : PACKAGE_NAME
* @className : Example2
* @description :
*/
public class Example2 {
public static void main(String[] args) {
// 素数个数
int count = 0;
System.out.println("100 ~ 200 之间的素数:");
for (int i = 100; i <= 200; i++) {
// 默认非素数
boolean isPrime = false;
for (int j = 2; j <= (int) Math.sqrt(i); j++) {
// 一旦能除尽,则说明不是素数,直接跳出循环
if (i % j == 0) {
isPrime = true;
break;
}
}
// 如果是素数,则素数个数 + 1,然后打印出该素数
if (isPrime == false) {
count++;
System.out.print(i + "\t");
// 每行打印 5 个素数,
if (count % 5 == 0) {
System.out.println();
}
}
}
System.out.println("\n素数总个数:" + count);
}
}
result
Example 3
topic
Print out all "daffodil numbers". The so-called daffodil numbers refer to a three-digit number whose cubic sum of each number is equal to itself, for example: $153=1*1*1+5*5*5+3* 3*3$, so 153 is the number of daffodils.
analyze
Because the daffodil number is a three-digit number, its range is 100 ~ 999, and then find the units, tens, and hundreds of the three-digit number, and finally determine whether the sum of their cubes is equal to the three-digit number. That's it.
accomplish
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @date : 2021/6/1 13:53
* @project : Java 编程实例
* @package : PACKAGE_NAME
* @className : Example3
* @description :
*/
public class Example3 {
public static void main(String[] args) {
System.out.println("水仙花数:");
for (int i = 100; i < 1000; i++) {
// 个位
int one = i % 10;
// 十位
int ten = i % 100 / 10;
// 百位
int hundred = i / 100;
// 计数
int count = 0;
if (one * one * one + ten * ten * ten + hundred * hundred * hundred == i) {
count++;
// 打印水仙花数,每 5 个换行输出
System.out.print(i + "\t");
if (count % 5 == 0) {
System.out.println();
}
}
}
}
}
result
Example 4
topic
Decompose a positive integer into prime factors, such as input 90, print out: 90 = 2 * 3 * 3 * 5
analyze
To decompose a number num into prime factors, you should first find the smallest prime number prime, and then proceed as follows:
- If the prime number is typed as num, it means that the decomposition of the prime factor is completed, and you can print it;
- If prime! = num, but num is divisible by prime, then print prime, then divide num by the quotient of prime as a new positive integer prime, repeat the first step;
- If num is not divisible by prime, replace prime with prime + 1, and repeat the first step;
accomplish
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @date : 2021/6/1 14:12
* @project : Java 编程实例
* @package : PACKAGE_NAME
* @className : Example4
* @description :
*/
public class Example4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个数");
int num = scanner.nextInt();
System.out.print(num + " = ");
// 最小的质因数
int prime = 2;
while (prime <= num) {
// num == prime,直接打印
if (num == prime) {
System.out.println(num);
break;
// 能除尽时
} else if (num % prime == 0) {
System.out.print(prime + " * ");
num = num / prime;
} else {
// 除不尽时
prime = prime + 1;
}
}
}
}
result
Example 5
topic
Use the nesting of conditional operators to complete: students with grades >= 90 are represented by A, those with scores between 60 and 89 are represented by B, and those with scores below 60 are represented by C;
analyze
You can use the ternary operator directly;
accomplish
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @date : 2021/6/1 22:10
* @project : Java 编程实例
* @package : PACKAGE_NAME
* @className : Example5
* @description :
*/
public class Example5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入成绩");
int score = scanner.nextInt();
char grade;
grade = score >= 90 ? 'A' : score >= 60 ? 'B' : 'C';
System.out.println("等级为:" + grade);
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。