# [basic skills of Java programming] output diamond and recursive algorithm.

Keywords: Java

### Twentieth questions

Print out the following patterns
(diamond with odd n rows)
It's like this~

```*
***
*****
*******
*****
***
*
```
``````public class Lianxi20 {
public static void main(String[] args) {
int h=7,w=7;
for(int i=0;i<(h+1)/2;i++){
for(int j=0;j<w/2-i;j++){
System.out.print(" ");
}
for(int k=1;k<(i+1)*2;k++){
System.out.print("*");
}
System.out.println();
}
for(int i=1;i<=h/2;i++){
for(int j=1;j<=i;j++){
System.out.print(" ");
}
for(int k=1;k<=w-i*2;k++){
System.out.print("*");
}
System.out.println();
}
}
}
``````

### Question 21

There is a sequence of fractions: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Sum the first 20 terms of this sequence.

Code:

``````public class lianxi20 {
public static void main(String[] args) {
double sum=0.0;
int x=2,y=1,t;
for(int i=1;i<=20;i++){
sum = sum+(double)x/y;
t=y;
y=x;
x=y+t;
}
System.out.println(sum);
}
}
``````

Here's the second way, recursively: because the law of numerator and denominator is Fibonacci sequence~

Code:

``````public class lianxi23 {
public static void main(String[] args) {
double sum=0.0;
for(int i=2;i<=21;i++){
sum = sum+(double)f(i+1)/f(i);
System.out.println(f(i+1)+"/"+f(i));
}
System.out.println(sum);
}
public static int f(int a){
if(a==1||a==2){
return 1;
}else{
return f(a-1)+f(a-2);
}
}
}
``````

### Question 22

Use recursive method to find n! (if n=5, then find 5!).

Code:

``````public class lianxi22 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(Recursion(n));

}
public static int Recursion(int n){
if(n==1||n==0){
return 1;
}else{
return n*Recursion(n-1);
}
}
}
``````

### Question 23

Five people sat together and asked the fifth how old he was? He said he was two years older than the fourth. Asked about the age of the fourth person, he said he was two years older than the third. Ask the third person and say that he is two years older than the second. Ask the second person,
Two years older than the first. Finally asked the first person, he said it was 10 years old. How old is the fifth person?

Code:

``````public class lianxi23 {
public static void main(String[] args) {
int n = 5;
System.out.println(f(5));
}
public static int f(int n ){
if(n==1) {
return 10;
}else{
return 2+f(n-1);
}
}
}
``````

At a glance, I thought of recursion properly. I wrote the above code and was very proud of it. Then ~ I found that this problem can be done with a simpler for loop~

Sure enough, I use recursion to use inertia. I don't know if my friends have this kind of time. I finished writing the code with difficulty. Looking up, the little brother next door not only did the problem, but also the logic was super simple~

Code:

``````public class lianxi25 {
public static void main(String[] args) {
int sum = 10;
for(int i=1;i<5;i++){
sum=sum+2;
}
System.out.println(sum);
}
}
``````

I have walked the most road, some is too the teacher's routine. Not only to write code to prevent bug s, but also to be aware of the sinister intentions of the teacher, my heart is so tired

You want to learn java! I sorted out a set of learning from the most basic Java entry level to Java framework content, to every little partner who wants to learn Java, want to get information, can pay attention to WeChat public number "fast learning Java" Oh, here is little white gathering place, welcome the beginner and advanced junior partner.

Pay attention to WeChat public number: learn Java quickly   Posted by frabble on Wed, 04 Dec 2019 07:50:30 -0800