2020/07/13-Java loop structure exercises (print Yang Hui triangle, print all odd numbers composed of 0-7, there is no repetition of numbers in each digit)
1. Print out Yang Hui triangle (10 lines are required to be printed as shown below)
- Program analysis:
- 1
- 1 1
- 1 2 1
- 1 3 3 1
- 1 4 6 4 1
- 1 5 10 10 5 1
- Ideas:
- First of all, the first number in each row is 1
- The law of each line of numbers is: 1; i/1; i(i-1)/1 2; (i-1)(i-2)/2 3;... int i =0
- Important: Analyze the relationship between the characteristics of each row of numbers and i, and also pay attention to the initial value of i is set to 0
- For example, for row 3: 1, 2, 1
- Ordinance v = 1; a = i,
- i=2 represents the third row, v=v*aa–; output: v=v/j, where int j=0, j=0 is defined, which means the first number in the first column, j=1 means one column The second number in
/**
*/
public class test15 {
public static void main(String[] args) {
int num = 5;
for (int i = 0; i < num; i++) {
int v = 1;
int a = i;
for (int j = 0; j <= i; j++) {
int b = j;
if(b==0)
System.out.print(1+" ");
else {
System.out.print((v = v / j) + " ");
}
v=v*a;
a--;
}
System.out.println();
}
}
}
2. Find the odd number of 0-7
analysis of idea:
-
Only odd numbers when the ones place is 1, 3, 5, 7
-
Calculate the odd numbers of 2 digits, 3 digits, 4 digits..., for example:
-
With 2 digits, there are 6*4 possibilities. Because the highest digit (first digit) cannot be 0, it is 7-1=6 possibilities
-
With 3 digits, there are 6 6 4 types, the first digit and the ones digit are a number, and the second digit is 8-2=6 types.
-
4 digits, there are 6 6 5*4 possibilities
-
Therefore, it should be written separately for 2 digits, and the digits after 2 can be written according to the circulation law
*/
public class test16 {
public static void main(String[] args) {
int total = 4;
for (int i = 2; i <=8 ; i++) {
//外层循环定义位数
int sum = 4;
for (int j = 1; j < i; j++) {
if (j==1){
sum=sum*6;
}else {
sum=sum*(8-j);
}
}
total+=sum;
}
System.out.println(total);
}
}
3. Find the positive difference between prime numbers and non-prime numbers within 1000
- Ideas:
- 2 is a prime number, 1 is neither a prime number nor a composite number
- Prime number + composite number + 1 = all numbers
- First find the prime numbers, then use the sum-the sum of prime numbers = the sum of non-prime numbers
public class khzy2 {
public static void main(String[] args) {
int j = 0;
int sum = 2;
int num = 0;
int s = 0;
for (int i = 1; i <1000 ; i++) {
num=num+i;
}
System.out.println(num);
for (int i = 2; i <1000 ; i++) {
for ( j = 2; j <i ; j++) {
s =i%j;
if (s==0){
break;
}
}
if (s!=0){
sum = sum + i;
}
}
System.out.println("质数:"+sum);
int n = num - sum;
int k = n - sum;
System.out.println(k);
}
}
4. Randomly generate 6 random numbers between 1 and 33, and a random number between 1 and 15. These 7 numbers cannot be repeated.
- Ideas:
- 1. int n = (int)(Math.random() 10); when generated is a random number between 0-9, so 34 will generate a random number between 0-33
- 2. Since each number cannot be repeated, add a || statement to ensure that every two numbers are not equal to each other.
- 3. If it is not equal, print out 7 numbers. If it is equal, use the contiune statement to exit this loop and continue to the next loop.
public class khzy1 {
public static void main(String[] args) {
int flag = 0;
do {
int num1 = (int) (Math.random() * 34);
int num2 = (int) (Math.random() * 34);
int num3 = (int) (Math.random() * 34);
int num4 = (int) (Math.random() * 34);
int num5 = (int) (Math.random() * 34);
int num6 = (int) (Math.random() * 34);
int num7 = (int) (Math.random() * 16);
if ((num1!=num2)||(num1!=num3)||(num1!=num4)||(num1!=num5)||(num1!=num6)
||(num1!=num7)||(num2!=num3)||(num4!=num2)||(num5!=num2)||(num6!=num2)
||(num7!=num2)||(num4!=num3)||(num5!=num3)||(num6!=num3)||(num7!=num3)||
(num4!=num5)||(num4!=num6)||(num4!=num7)||(num4!=num2)||(num5!=num6)||(num5!=num7)||(num6!=num7)
){
flag = 1;
System.out.println(num1+" "+num2+" "+num3+" "+num4+" "+num5+" "+num6+" "+num7+" ");
};
}while (flag == 0);
}
}