Exercises
1. Calculate the factorial of the input number
Loop and recursion
(1) Recursion
public static int func1(int n){ int res = 1; if(n==1){ return 1; } for (int i = 1; i <= n; i++) { res*=i; } return res; }
(2) Recursion
Recursive conditions:
- Recursive calls can be used to reduce the scale of the problem, and the new problem has the same form as the original problem. (calling itself)
- There is a simple scenario that allows recursion to add and exit in a simple scenario. (end recursion)
Three elements of recursion:
- There must be a situation where you can exit the program;
- Always trying to simplify a problem to a smaller scale;
- Parent and child questions cannot overlap.
public static void main(String[] args) { //Calculates the factorial value of the input data System.out.println("10! = "+func(10)); } public static int func(int num){ if(num==1){ return 1; }else{ return num*func(num-1);//The main step of recursion is to call itself } }
2. Exchange the values of two numbers (intermediate variables are not allowed)
public static void func(int num1,int num2){ num1 = num1+num2;//num1 = 1,num2 = 2 num2 = num1-num2;//num1 = 3,num2 = 1 num1 = num1-num2;//num1 = 2,num2 = 1 System.out.println("num1 = "+num1); System.out.println("num2 = "+num2); }
3. Output the maximum and minimum of the three numbers
Conclusion: 1. Compare the first two values to obtain a larger number. 2. In the same step as the first step, compare the larger one with the third one to obtain the maximum value.
//Custom method int[] arr = new int[3]; Scanner sc = new Scanner(System.in); for(int i = 0;i<arr.length;i++){ System.out.println("Please enter page"+(i+1)+"Integer:"); int num = sc.nextInt(); arr[i] = num; } int num = arr[0]>arr[1]?arr[0]:arr[1]; int max = num>arr[2]?num:arr[2]; System.out.println("The maximum value is:"+max); //Java internal methods int[] arr = new int[3]; Scanner sc = new Scanner(System.in); for(int i = 0;i<arr.length;i++){ System.out.println("Please enter page"+(i+1)+"Integer:"); int num = sc.nextInt(); arr[i] = num; } int num = Math.max(arr[0],arr[1]); int max = Math.max(num,arr[2]); System.out.println("The maximum value is:"+max); //Use ternary operator int k1 = 1,k2 = 3; int num = k1>k2?++k1:k2++;//Since expression 1 is true, expression 2 is executed, but expression 3 is not executed System.out.println(num+":"+k1+"=="+k2);
4. Output an odd number of 1-100 (6 per line)
public static void main(String[] args){ int count = 0; for(int i = 1;i<=100;i++){ if(i%2!=0){ System.out.print(i+"\t"); if(++count%6==0){ System.out.println(); } } } }
5.1-100 summation (three cyclic writing methods)
Insert the code slice here
6.1-100 odd sum
int res = 0; for(int i = 1;i<100;i=i+2){ res+=i; } int num = 0; for(int k = 1;k<=100;k++){ if(k%==0)continue; num+=k; }
7. Output 1-100 numbers that can be divided by 3, and output 6 for each line
for(int i = 3;i<100;i=i+3){ System.out.println(i); } int count = 0; for(int k = 1;k<=100;k++){ if(k%3==0){ System.out.print(k+"\t"); if(++count%6==0) System.out.println(); } }
8. Find all numbers within 100 that can be divided by 3 but cannot be divided by 5, and output 6 in each line
for(int i = 3;i<100;i=i+3){ System.out.println(i); } int count = 0; for(int k = 1;k<=100;k++){ if(k%3==0||k%5!=0){ System.out.print(k+"\t"); if(++count%6==0) System.out.println(); } }
9. Print out the number of all daffodils
Narcissus: the value of a three digit number. The sum of the three powers of the numbers in each digit is equal to itself.
int count = 0; for(int i = 100;i<1000;i++){ int k1 = i%10;//Single digit int k2 = i/100;//Hundreds int k3 = i/10%10;//Ten digits if(i==(k1*k1*k1+k2*k2*k2+k3*k3*k3)){ //Math.pow(k1,3) System.out.print(i+"\t");//Test case 153 if(++count%3==0) System.out.println(); } }
10. Judge whether a number is a prime number
Prime number: refers to a natural number greater than 1, which has no other factors except 1 and itself.
public static boolean suShu(int i){ boolean suShu = true;//Judge whether i is a prime number for(int k = 2;k<i/2;k++){ suShu = false; break; } return suShu; }
11. Program to find all prime numbers in natural numbers 101-205
public static void main(String[] args){ int count = 0; for(int i = 101;i<=205;i++){ //Judge whether i is a prime number boolean b = suShu(i); if(b){ System.out.print(i+"\t"); if(++count%6==0)//6 banks for output System.out.println(); } } } public static boolean suShu(int i){ boolean suShu = true;//Judge whether i is a prime number for(int k = 2;k<i/2;k++){ suShu = false; break; } return suShu; }
12. Enter two integers m and n to find their maximum common divisor and minimum common multiple
Maximum common divisor: refers to the largest common divisor of two or more integers.
Least common multiple
public static void main(String[] args) { int m, n;// Declare variable Scanner sc = new Scanner(System.in); System.out.println("Enter integer 1:"); while (true) { // It is impossible to predict how many times the user needs to input to ensure the correctness and effectiveness of the data m = sc.nextInt(); if (m < 2) System.out.println("The data entered is illegal, please re-enter"); else break; } System.out.println("Enter integer 2:"); while (true) { n = sc.nextInt(); if (n < 2) System.out.println("The data entered is illegal, please re-enter"); else break; } //It is necessary to judge the legitimacy of the data entered by the user to ensure that there will be no special circumstances in the process of application execution, so as to improve the robustness of the code int gys = gongYueShu(m, n); if (gys > 1) System.out.println(m + "and" + n + "The maximum common divisor of is:" + gys); else System.out.println(m + "and" + n + "Mutually prime numbers"); int gbs = gongbeishu(m,n); System.out.println("The common multiple is:"+gbs); } // Get the maximum common divisor of num1 and num2 integers. If 1 is returned, it means that they are prime numbers //Common divisor public static int gongYueShu(int num1, int num2) { int res = 1; int end = Math.min(num1, num2); for (int i = end; i > 1; i--) { // In fact, you can continue to optimize to reduce the number of cycles if (num1 % i == 0 && num2 % i == 0) { res = i; break; // If the first divisor is obtained, there is no need to continue the calculation. What is obtained is the maximum common divisor } } return res; } //Common multiple public static int gongbeishu(int num1, int num2){ int res = 1; int end = Math.min(num1, num2); for (int i = end; i > 1; i++) { // In fact, the minimum value can still be optimized to reduce the number of cycles if (i % num1 == 0 && i % num2 == 0) { res = i; break; // If the first divisor is obtained, there is no need to continue the calculation, which is the maximum common divisor } } return res; }
13. How many integers are there between 1000 and 50000? The sum of their digits is 5, which are those numbers respectively, and count the number of integers that meet the conditions
public static void main(String[] args) { //How many integers are there between 1000 and 50000? The sum of each digit is 5, which are those numbers respectively, and count the number of integers that meet the conditions for (int i = 100; i < 50000; i++) { //If it is 3 digits if (i >= 100 && i < 1000) { int k1 = i % 10;//Single digit int k2 = i / 100;//Hundreds int k3 = i / 10 % 10;//Ten digits if (k1 + k2 + k3 == 5) { System.out.println(i); } } else //If it is 4 digits if (i >= 1000 && i < 10000) { int k1 = i % 10;//Single digit int k2 = i / 1000;//thousands int k3 = i / 10 % 10;//Ten digits int k4 = i / 100 % 10;//Hundreds if (k1 + k2 + k3 + k4 == 5) { System.out.println(i); } } else //If it is 5 digits if (i >= 10000) { int k1 = i % 10;//Single digit int k2 = i / 10000;//thousands int k3 = i / 10 % 10;//Ten digits int k4 = i / 100 % 10;//Hundreds int k5 = i / 1000 % 10;//thousands if (k1 + k2 + k3 + k4 + k5 == 5) { System.out.println(i); } } } }
14. There are 80 heads and 208 feet in the same cage. How many chickens and rabbits are there respectively
for (int i = 0; i <= 80; i++) { int k = 80-i; if(i*2+k*4==208) System.out.println("Among them"+i+"A chicken,"+k+"Rabbit"); }
15. A chicken Weng is worth five, a chicken mother is worth three, and a chicken chick is worth three. When you buy a hundred chickens for a hundred dollars, ask the chicken owner, the chicken mother and the chicken chick about the geometric description. It is a reasonable evaluation of the number of cycles
for (int i = 0; i <= 20; i++) {//Number of cocks for (int j = 0; j <= 300; j+=3) {//Number of chicks int k = 100-i-j;//Number of hens if(k<0) continue; if(i*5+j/3+k*3==100) System.out.println("cock:"+i+",hen:"+k+",chick:"+j); } }