# 1.13 daily operation

Posted by BornForCode on Thu, 13 Jan 2022 17:50:36 +0100

Title List

1. Get an integer through Scanner}, and then use while to calculate the factorial of the integer

2. Implement 9 * 9 multiplication table

3. There are 30 people, including men, women and children. They spend a total of 50 shillings in a hotel, including 3 shillings for each man, 2 shillings for each woman and 1 shilling for each child. How many men, women and children?

4. Tip:

5,

6. Print the number between 1 and 100. If the number is a multiple of 3 or 5, it will be ignored

7. Number of daffodils:

8. Bubble sorting

9. Select sort

10. Half search (binary search method) [premise array should be sorted in positive order]

11. 1 2 3 4 5 6 7 --- 7654321 (half reversal)

12. Girlfriend said (recursive)

13. One hundred chickens and one hundred dollars: buy one hundred chickens for one hundred yuan, one Rooster for five yuan, one hen for three yuan and three chicks for one yuan

14. Suppose your monthly income is 6000. In addition to your usual expenses, you leave 2000 yuan for investment every month.

# 1. Pass Scanner Get an integer, and then use while to calculate the factorial of the integer

The factorial of N is equal to N * (N-1) * (N-2) ** one

3==>1*2*3

3==>3*2*1

How many cycles does it need to go through?

## code:

```package com.day0113;

import java.util.Scanner;

public class Test1 {

public static void main(String[] args) {
// Get an integer through Scanner, and then use while to calculate the factorial of this integer
// The factorial of N is equal to N * (N-1) * (N-2) ** one
Scanner sc = new Scanner(System.in);
// Get a value
int n = sc.nextInt();
System.out.print(n);
// Define a result
int result = n;
// Cycle product 5 * 4 * 3 * 2 * 1
while(n>1) {
//iteration
n--;
//Find product
result = result*n;
}
//Printout results
System.out.println("The factorial result of is:"+result);
}

}
```

# 2. Implement 9 * 9 multiplication table

1*1=1

2*1=2 2*2=2

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

9 rows of data. Each row corresponds to n columns of data?

The first loop traverses 9 rows of data (outer loop)

The second loop traverses n columns of data in each row (inner loop)

## code:

```package com.day0113;

public class Test2 {

public static void main(String[] args) {
// 9 * 9 multiplication table
//Outer loop: traverse 9 times -- equivalent to printing the multiplication table of each row
for(int n=1;n<=9;n++) {
//Inner loop: traverse n times -- print n multiplication tables of the current row
for(int i=1;i<=n;i++) {
//Format output
System.out.printf("%d*%d=%d \t",i,n,(i*n));
}
//Line feed
System.out.println();
}
}

}
```

# 4. Tip:

System.out.print("*");// Output asterisk does not wrap

System.out.println();// Line feed

System.out.print(" ");// Output space

6* --1

5*** --3

4***** --5

3******* --7

2********* --9

1*********** --11

0************* --13

law:

Asterisk of each line: 2*n-1;

Space in each row: total rows - sequence number of the current row

The total number of rows is 7

# 5,

6* --1

5*** --3

4***** --5

3******* --7

2********* --9

1*********** --11

0************* --13

***********

*********

*******

*****

***

*

Analysis: the upper part has been completed, only the lower part needs to be added

# 6. Print the number between 1 and 100. If the number is a multiple of 3 or 5, it will be ignored

## code:

```package com.day0113;

public class ForControllerTest {

public static void main(String[] args) {
//Print the number between 1 and 100. If the number is a multiple of 3 or 5, it will be ignored
for(int i=1;i<=100;i++) {
if(i%3==0||i%5==0) {
continue;
}
System.out.print(i+"\t");
}
System.out.println();

}

}
```

# 7. Daffodils number*

Find out that the number of all daffodils must be the cube of each three digits (100 ~ 999), and the sum is exactly the number itself. For example, 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3. The current number = hundreds ^ 3 + tens ^ 3 + bits ^ 3

## code:

```package com.day0113;

public class ForControllerTest {

public static void main(String[] args) {
//Definition of daffodils: find all daffodils
for(int i=100;i<=999;i++) {
//Find the value of one hundred and ten
int bai = i/100;
int shi = (i/10)%10;
int ge = i%10;
//Calculate and judge whether the conditions are met
if((bai*bai*bai+shi*shi*shi+ge*ge*ge)==i) {
System.out.println(i);
}
}
}

}
```

# 8. Bubble sorting*

It repeatedly visits the element column to be sorted, compares two adjacent elements in turn, and exchanges them if the order (e.g. from large to small and from Z to A) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is, the element column has been sorted.

The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence (in ascending or descending order) through exchange, just as the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, so it is called "bubble sorting".

## code:

```package com.day0113;

import java.util.Arrays;

public class ArrayDemo1 {

public static void main(String[] args) {
// Creating an array object initializes the reference type by default. The default is null
/*
* int[] arr = new int[6]; for (int i : arr) { System.out.println(i); }
*/

// After each operation, determine a bit [take a bubble]
int[] arr = { 5, 101, 978, 43, 45, 64, 1, 65 };
// After a bit is not determined, the comparison times of the next operation are reduced by one
// How many times does it need to be operated? (n-1)； How many rounds (n-i) are compared for each operation?
// How many times does it need to be operated? (n-1
for (int n = 0; n < arr.length - 1; n++) {// 0~6
// How many rounds (n-i) are compared for each operation?
for (int i = 0; i < arr.length - n - 1; i++) {// 0~6
// When the front is larger than the back, it needs to be exchanged
if (arr[i] > arr[i + 1]) {
int item = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = item;
}
}
}

//ergodic
for (int i : arr) {
System.out.println(i);
}
}

}
```

# 9. Select sort*

Selection sort is a simple and intuitive sorting algorithm. Its working principle is: select the smallest (or largest) element from the data elements to be sorted for the first time, store it at the beginning of the sequence, then find the smallest (large) element from the remaining unordered elements, and then put it at the end of the sorted sequence. And so on until the number of all data elements to be sorted is zero. Selective sorting is an unstable sorting method.

1-2 1-3 1-4 ....

2-3~7

3-4~7

## code:

```package com.day0113;

import java.util.Arrays;

public class ArrayDemo1 {

public static void main(String[] args) {
// Creating an array object initializes the reference type by default. The default is null
/*
* int[] arr = new int[6]; for (int i : arr) { System.out.println(i); }
*/

//Select sort
//Time complexity O(N)
//Defines an index that stores the location of the current minimum value
int index = 0;
for (int n = 0; n < arr.length - 1; n++) {// 0 ~ 6 starting position
//Initializes the index of the starting position
index = n;
// How many rounds (n-i) are compared for each operation? [inner loop as later traversal] comparison between starting position n and later
for (int i = n+1; i < arr.length; i++) {// 1~7 2~7 3~7
// When the following value is less than the index position, the index is assigned to index
if (arr[i] < arr[index]) {
index = i;
}
}
//Exchange: exchange the minimum and starting position values
int item = arr[n];
arr[n] = arr[index];
arr[index] = item;
}
//Time complexity O(N^2)
for (int n = 0; n < arr.length - 1; n++) {// 0 ~ 6 starting position
// How many rounds (n-i) are compared for each operation? [inner loop as later traversal] comparison between starting position n and later
for (int i = n+1; i < arr.length; i++) {// 1~7 2~7 3~7
// When the starting position is greater than the following, it needs to be exchanged
if (arr[n] > arr[i]) {
int item = arr[i];
arr[i] = arr[n];
arr[n] = item;
}
}
}
//ergodic
for (int i : arr) {
System.out.println(i);
}
}

}
```

# 10. Half search (binary search method) [premise array should be sorted in positive order]*

[1, 5, 43, 45, 64, 65,99, 101, 978] key value 99

ps:

1. Start from the middle of the array. If the number of searches is > the middle value, find it later; If the number of

2. If it cannot be found for the first time, continue to find the two points from the middle of the back (front) until the start position > the end position, and then exit the cycle

## code:

```package day0113;

import java.util.Arrays;

public class Arraydemo3 {

public static void main(String[] args) {
int[] arr= {1, 5, 43, 45, 64, 65,99, 101, 978};
//Sorting using tool classes
Arrays.sort(arr);
//Implement binary query logic operation
int key =99;
//Intermediate value index
int index =0;
//Query range
int start =0;
int end =arr.length;
//Set a flag bit:
boolean isFind = false;
//loop
while(start<=end) {
//Calculate intermediate index
index =(end+start)/2;
//Judge [= =, >, <]
if(key==arr[index]) {
System.out.println("The target index found is:"+index);
//end
isFind=true;
break;
}else if(arr[index]>key) {
end=index-1;
}else {
start=index+1;
}
}
if(!isFind) {
}
}

}
```

# 11. 1 2 3 4 5 6 7 --- 7654321 (half reversal)

-- half reverse (reverse array order)

## code:

```package day0113;

import java.util.Arrays;

public class Homeword1 {

public static void main(String[] args) {
// 1 2 3 4 5 6 7--->7654321
// -- half reverse (reverse array order)
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
// Number of operations
int num = arr.length / 2;
// Switching operation: 1 and 7, 2 and 6, 3 and 5
for (int i = 0; i <= num; i++) {
int item = arr[i];
arr[i] = arr[arr.length - i - 1];// Change the first number and the last one.
arr[arr.length - i - 1] = item;
}
System.out.println(Arrays.toString(arr));

}

}
```

# 12. Girlfriend said (recursive)

Give me a penny the first day

Give me two cents the next day

Give me four cents on the third day

Give me eight cents on the fourth day

And so on: give me enough for a month [30 days] and I'll marry you, or I won't want you. [please calculate the total cost]

## code:

```package day0113;

public class Homeword2 {

public static void main(String[] args) {
// Said the girlfriend
// Give me a penny the first day
// Give me two cents the next day
// Give me four cents on the third day
// Give me eight cents on the fourth day
// And so on: give me enough for a month [30 days] and I'll marry you, or I won't want you. [please calculate the total cost]
int num = 1;
int n = 0;
// A total of 30 cycles
for (int i = 1; i <= 30; i++) {
n += num;// Total money per day
num *= 2;// Amount per day multiplied by 2
System.out.println("The first" + i + "day");
}
System.out.println("in total" + n + "Penny, I want to marry you!");
}

}
```

# 13. One hundred chickens and one hundred dollars: buy one hundred chickens for one hundred yuan, one Rooster for five yuan, one hen for three yuan and three chicks for one yuan

Tip: use for loop nesting

Conditions for establishment: the total price is 100 And the total number is 100

Analysis: 100 yuan can buy up to 20 cocks

100 yuan can buy up to 33 hens

100 yuan can buy up to 100 chickens

## code:

```package day0113;

public class Homeword3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
// The external circulation setting of 100 can buy up to 20 cocks, within 20 rounds
for (int i = 1; i <= 20; i++) {
// The internal circulation setting of 100 can buy up to 33 hens within 33 rounds
for (int j = 1; j <= 33; j++) {
// The setting conditions meet the total number of 1.100 chickens at the same time minus the sum of the number of hens and roosters and other chicks
// 2. The total price of three kinds of chickens is equal to 100
if ((100 - i - j) % 3 == 0 && 5 * i + 3 * j + (100 - i - j) / 3 == 100) {
System.out.println("cock" + i + "Only, hen" + j + "Chicken, chicken" + (100 - i - j) + "Only.");
}

}
}
}

}
```

# 14. Suppose your monthly income is 6000. In addition to your usual expenses, you leave 2000 yuan for investment every month.

It is assumed to achieve an annual return on investment of 20%. With the rhythm of investing 2000 yuan per month, how many years of continuous investment can the total income reach 1 million (compound interest is calculated according to 24000 investment per year, not according to monthly interest)

Compound interest formula: F = P * ((1 + R) ^ n);

F. final revenue of this year

p. principal

r) annual interest rate

How ma n y years have you saved

Calculate total income = = > previous income + (this year's deposit * compound interest)

First year 24000 * ((1 + 0.2) ^ 1) = 28800

28800 + 24000 * (1.2) ^ 2 = 63360 in the second year

The third year 63360 + 24000 ((1 + 0.2) ^ 3) = 104832

give an example:

1.2^2

Math.pow(1.2,2);

## code:

```package day0113;

public class Homeword4 {

public static void main(String[] args) {
// Annual principal amount
int n = 24000;
// interest rate
double r = 1.2;
// particular year
int year = 1;
// Total revenue
double num = 0;
// The judgment condition is true
while (true) {
// Calculate annual profit plus principal
num = num + (n * Math.pow(r, year));
System.out.printf("The first%d Total annual income:%f \n", year, num);
// When the amount is greater than 1000000, the year is output
if (num >= 1000000) {
System.out.printf("The first%d 100 in W", year);
break;
}
// Year self increase
year++;
}

}

}
```

Topics: Java Eclipse