Example 6
subject
Enter two positive integers m and n to find their maximum common divisor and minimum common multiple.
analysis
In the cycle, as long as the divisor is not equal to 0, divide the larger number by the smaller number, take the smaller number as the larger number of the next cycle, and the remainder obtained as the smaller number of the next cycle. In this way, the cycle knows that the value of the smaller number is 0, and returns the larger number. This number is the largest common divisor, and the minimum common multiple is the product of two numbers divided by the largest common divisor.
realization
import java.util.Scanner; /** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @Official account: Village rain * @website : https://cunyu1943.github.io * @date : 2021/6/1 22:23 * @project : Java Programming example * @package : PACKAGE_NAME * @className : Example6 * @description : */ public class Example6 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("input m, n"); int m = scanner.nextInt(); int n = scanner.nextInt(); int divisor = maxCommonDivisor(m, n); int multiple = m * n / maxCommonDivisor(m, n); System.out.println("greatest common divisor: " + divisor); System.out.println("Least common multiple: " + multiple); } public static int maxCommonDivisor(int a, int b) { int max, min; max = (a > b) ? a : b; min = (a < b) ? a : b; if (max % min != 0) { return maxCommonDivisor(min, max % min); } else { return min; } } }
result
Example 7
subject
Enter a line of characters and count the number of English letters, spaces, numbers and other characters;
analysis
Traverse the string, then look at the category of each character and count them respectively;
realization
import java.util.Scanner; /** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @Official account: Village rain * @website : https://cunyu1943.github.io * @date : 2021/6/1 23:04 * @project : Java Programming example * @package : PACKAGE_NAME * @className : Example7 * @description : */ public class Example7 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Input string"); String str = scanner.nextLine(); int character = 0; int digit = 0; int blank = 0; int others = 0; // Traverse the string, and then count the characters in it for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == ' ') { blank++; } else if (ch >= '0' && ch <= '9') { digit++; } else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') { character++; } else { others++; } } System.out.println("Number of spaces:" + blank); System.out.println("Number of English letters:" + character); System.out.println("Number of digits:" + digit); System.out.println("Other characters:" + others); } }
result
Example 8
subject
seek s = a + a a + a a a + ... + a a ... a s = a + aa + aaa + ... + aa...a S = the value of a + aa + AAA +... + aa... A, where a a A is a number, such as 2 + 22 + 222 +... + 22... 2
analysis
The first is to input separately a a a and count, and then cycle to get the maximum number, accumulate and sum each time, and then move forward one bit.
realization
import java.util.Scanner; /** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @Official account: Village rain * @website : https://cunyu1943.github.io * @date : 2021/6/2 16:25 * @project : Java Programming example * @package : PACKAGE_NAME * @className : Example8 * @description : */ public class Example8 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Input times"); int count = scanner.nextInt(); System.out.println("input a"); int a = scanner.nextInt(); int sum = 0; int i = 0; // Cache mantissa int tmp = 0; while (i < count) { tmp += a; sum += tmp; a *= 10; i++; } System.out.println("Final and final:" + sum); } }
result
Example 9
subject
If a number is exactly equal to the sum of its factors, then this number is called "perfections". For example, 6 = 1 + 2 + 3, program to find all perfections within 1000.
analysis
Find each factor of num, then use a variable sum to calculate the sum of the factors of a number num, and finally judge whether sum and num are equal. If they are equal, it means that the number is a perfect number, if they are not equal, it means that it is not.
realization
/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @Official account: Village rain * @website : https://cunyu1943.github.io * @date : 2021/6/2 16:36 * @project : Java Programming example * @package : PACKAGE_NAME * @className : Example9 * @description : */ public class Example9 { public static void main(String[] args) { System.out.println("1000 Completion within:"); int count = 0; for (int i = 1; i <= 1000; i++) { if (check(i)) { count++; System.out.print(i + "\t"); } } System.out.println("\n1000 Number of completions within:" + count); } /** * Judge whether a number is complete * * @param num * @return true Completion; false incomplete */ public static boolean check(int num) { int sum = 0; // Find and sum the factors of num for (int i = 1; i < num; i++) { if (num % i == 0) { sum += i; } } // Judge whether the sum of each factor is equal to this number if (sum == num) { return true; } return false; } }
result
Example 10
subject
A ball falls freely from a height of 100 meters. After each landing, it bounces back half of the original height and then falls. How many meters does it pass on the 10th landing? What is the height of the 10th rebound?
analysis
The distance and height of the first fall are 100 m, and then cycle for the next 9 times. The height of each fall is the general height of the last fall, and the distance is the sum of the previous journey plus the height of this fall.
realization
/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @Official account: Village rain * @website : https://cunyu1943.github.io * @date : 2021/6/2 16:46 * @project : Java Programming example * @package : PACKAGE_NAME * @className : Example10 * @description : */ public class Example10 { public static void main(String[] args) { double height = 100.0d; double sum = 100.0d; for (int i = 1; i < 10; i++) { // The distance passed is equal to the previous distance plus the current falling height sum += height; // The current falling height is half of the previous one height /= 2; } System.out.println("10th rebound height:" + height / 2); System.out.println("10 Total journey:" + sum); } }