Basic programming questions (6~10)
06, find the greatest common divisor and the least common multiple
Question: Enter two positive integers m and n, and find the greatest common divisor and least common multiple.
6.1, program analysis
- Greatest common divisor The greatest common divisor of
two numbers is less than the smallest number, and the result of the common divisor taking the remainder of the two numbers is 0. - The least common multiple
1, the least common multiple is the product of two numbers divided by the greatest common divisor.
2. The least common multiple is greater than the largest number and less than the product of two numbers, and the remainder of the two numbers to the least common multiple is 0.
6.2, code implementation
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入两个数m和n:");
int m = input.nextInt();
int n = input.nextInt();
// 判断两数的大小
int min = m > n ? n : m;
int max = m > n ? m : n;
// 输出最大公约数和最小公倍数
for (int i=min; i > 0; i--) {
if (m % i == 0 && n % i == 0) {
System.out.println("最大公约数是:" + i);
System.out.println("最小公倍数是:"+(n*m)/i);
break;
}
}
}
}
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入两个数m和n:");
int m = input.nextInt();
int n = input.nextInt();
// 判断两数的大小
int min = m > n ? n : m;
int max = m > n ? m : n;
// 输出最大公约数
for (int i=min; i > 0; i--) {
if (m % i == 0 && n % i == 0) {
System.out.println("最大公约数是:" + i);
break;
}
}
// 输出最小公倍数
for (int i = max; i <= n * m; i++) {
if (i % m == 0 && i % n == 0) {
System.out.println("最小公倍数是:" + i);
break;
}
}
}
}
6.3、Science Classroom
Greatest common divisor
The greatest common divisor, also known as the greatest common divisor, or the greatest common divisor, refers to the largest of the divisors shared by two or more integers. a,b
The greatest common divisor of is recorded as (a,b)
, similarly, a,b,c
the greatest common divisor is recorded as (a,b,c)
, and the greatest common divisor of multiple integers also has the same sign. There are many ways to find the greatest common divisor, the common ones arePrime factor decomposition method, short division method, toss and turn division method, more phase subtraction method。
Detailed address analysis: the greatest common divisor-Baidu Encyclopedia
Least common multiple
The multiples common to two or more integers are called their common multiples, and the 0
smallest common multiple except that is called the least common multiple of these integers. a,b
The least common multiple of an integer is recorded as [a,b]
, similarly, a,b,c
the least common multiple is recorded as [a,b,c]
, and the least common multiple of multiple integers also has the same sign.
The solution isPrime factor decomposition method, formula method。
Detailed analysis address: least common multiple-Baidu Encyclopedia
07. Simple processing of strings
Topic: Enter a line of characters and count the number of English letters, spaces, numbers and other characters.
7.1, program analysis
Realization ideas
- Define an array with a length of 256 (0~255) to count the number of each character;
- The number of characters corresponding to the subscript of the array;
- Print out the number of statistics.
7.2, code implementation
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入字符串:");
String str = input.nextLine();
// 1、定义计数数组
int[] count = new int[256];
// 2、统计字符个数
for (int i = 0; i < count.length; i++) {
for (int j = 0; j < str.length(); j++) {
if (i == str.charAt(j)) {
count[i]++;
}
}
// 3、打印结果
if (count[i] > 0) {
System.out.print((char) i + "的个数: " + count[i] + "\n");
}
}
}
}
7.3、Science Classroom
7.3.1, the difference between next() and nextLine()
Java
The Scanner
methods in the class next()
and nextLine()
are drawing input stage of the input character, the difference between:
-
next()
Will not absorb the space/Tab
key before/after the character, only absorb the character, start to absorb the character (before and after the character is not counted) until it encounters the space/Tab
key/enter to stop the absorption; -
nextLine()
Absorb the space/Tab
key before and after the character ,Enter keyDeadline.
08、s=a+aa+aaa+aaaa+aa…a
Question: Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a number. For example, 2+22+222+2222+22222 (at this time, 5 numbers are added together), and the addition of several numbers is controlled by the keyboard.
8.1, program analysis
This question can be considered from two aspects: "characters" and "numbers":
- Treat a as characters.
Realization ideas
1. Please enter the repeated number [1-9]
2. Please enter the number of repeated additions
3. Determine whether the number is qualified or not, re-enter it
4. Define the sum variable
5. Definition Repeat the number of strings each time
6. Print the output string - Treat a as a number.
Realization ideas
1. Please enter the repeated number [1-9]
2. Please enter the number of repeated additions
3.22=2 10 to the 1st power + 2; 222 = 2 10 to the 2 power + 22 2222=2 10 to the 3rd power + 222
rule: the current nth digit is equal to 2 10 to the (n-1) power + previous number
4. Define the sum variable int sum = 0;
8.2, code implementation
Treat a as characters
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int n;
do {
//1.请输入重复的数字[1-9]
System.out.println("请输入重复的数字[1-9]:");
num = input.nextInt();
//2.请输入重复相加的个数
System.out.println("请输入重复相加的个数:");
n = input.nextInt();
} while (num < 1 || num > 9);//3.判断数字是否合格,不合格重新输入
//4.定义求和变量
int sum = 0;
//5.定义每次的重复数字串
String temp = "";
//6.打印输出的字符串
String str = "";
for (int i = 0; i < n; i++) {
temp +=""+num;
sum += Integer.valueOf(temp);
if(i<n-1)
str+=temp+"+";
else
str+=temp;
}
System.out.println(sum+"="+str);
}
}
Treat a as a number
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int count;
do {
//1.请输入重复的数字[1-9]
System.out.println("请输入重复的数字[1-9]:");
num = input.nextInt();
//2.请输入重复相加的个数
System.out.println("请输入重复相加的个数:");
count = input.nextInt();
} while (num < 1 || num > 9);//3.判断数字是否合格,不合格重新输入
//4.定义求和变量
int sum = 0;
int temp = num;
for (int i = 1; i <=count; i++) {
if(i<count)
System.out.print(temp+"+");
else
System.out.print(temp);
sum += temp;
temp = num * (int)Math.pow(10, i)+temp;
}
System.out.println("="+sum);
}
}
09. Finished
Question: If a number is exactly equal to the sum of all its factors, this number is called "complete number". For example, 6=1+2+3. Program to find all the numbers within 1000.
9.1, program analysis
- Realize the idea
1. Find all the factors of the number num and sum them (sum)
2. Compare num and sum
9.2, code implementation
public class Test {
public static void main(String[] args) {
int sum;
for (int i = 1; i <= 1000; i++) {
sum = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
sum += j;
}
}
if (i == sum) {
System.out.println(i);
}
}
}
}
10. The height of free fall
Question: A ball falls freely from a height of 100 meters, and rebounds back to half of its original height each time it hits the ground. When it falls again, how many meters does it travel when it hits the ground for the 10th time? How high is the 10th rebound?
10.1 Program analysis
- Realization idea
1. Define a loop, the number of loops is the number of rebounds
2. Define an intermediate variable, the height of each rebound
10.2 Code implementation
public class Test {
public static void main(String[] args) {
double height=100;//高度
double temp = 0;
double sum = height;//物体落体时经过的路程
for (int i = 0; i < 10; i++) {
sum += temp * 2;
temp = height / 2.0;
height = temp;
}
System.out.println("第" + 10 + "次落地时,共经过" + sum + "米,第" + 10 + "次反弹" + temp + "米。");
}
}