Classic java algorithm problem (III), final notes of Tencent redis

classic java algorithm problem iii

public class test {

public static void main (String[]args){

long k=0;

for(k=1;k<=100000l;k+

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

+)

if(Math.floor(Math.sqrt(k+100))==Math.sqrt(k+100) && Math.floor(Math.sqrt(k+168))==Math.sqrt(k+168))

System.out.println(k);

}

}

【 Program 14】 subject : Enter the date of the year , Judge the day as the day of the year ?

1. Program analysis : With 3 month 5 Day, for example , The first two months should be added up , And then add 5 Day is the day of the year , A special case , Leap year and input month is greater than 3 Consider an extra day .

import java.util.*;

public class test {

public static void main (String[]args){

int day=0;

int month=0;

int year=0;

int sum=0;

int leap;

System.out.print(“ Please enter year , month , Japan \n”);

Scanner input = new Scanner(System.in);

year=input.nextInt();

month=input.nextInt();

day=input.nextInt();

switch(month) / First, calculate the total number of days in the month before a certain month /

{

case 1:

sum=0;break;

case 2:

sum=31;break;

case 3:

sum=59;break;

case 4:

sum=90;break;

case 5:

sum=120;break;

case 6:

sum=151;break;

case 7:

sum=181;break;

case 8:

sum=212;break;

case 9:

sum=243;break;

case 10:

sum=273;break;

case 11:

sum=304;break;

case 12:

sum=334;break;

default:

System.out.println(“data error”);break;

}

sum=sum+day; / Plus the days of the day /

if(year%4000||(year%40&&year%100!=0))/ Decide if it's a leap year /

leap=1;

else

leap=0;

if(leap==1 && month>2)/ If it is a leap year and the month is greater than 2, The total number of days should be increased by one day /

sum++;

System.out.println(“It is the the day:”+sum);

}

}

【 Program 15】 subject : Output 9*9 formula .

1. Program analysis : Branch and column considerations , common 9 That's ok 9 Column ,i The control line ,j Control the column .

public class jiujiu {

public static void main(String[] args)

{

int i=0;

int j=0;

for(i=1;i<=9;i++)

{ for(j=1;j<=9;j++)

System.out.print(i+""+j+"="+ij+"\t");

System.out.println();

}

}

}

There is no repeated product ( Lower triangle )

public class jiujiu {

public static void main(String[] args)

{

int i=0;

int j=0;

for(i=1;i<=9;i++)

{ for(j=1;j<=i;j++)

System.out.print(i+""+j+"="+ij+"\t");

System.out.println();

}

}

}

Upper triangle

public class jiujiu {

public static void main(String[] args)

{

int i=0;

int j=0;

for(i=1;i<=9;i++)

{ for(j=i;j<=9;j++)

System.out.print(i+""+j+"="+ij+"\t");

System.out.println();

}

}

}

【 Program 16】 subject : The problem of monkeys eating peaches : The monkey picked some peaches on the first day , Half eaten immediately , It's not addictive , Another one The next morning I ate half of the rest of the peaches , Another one . Every morning I eat the rest of the day before One and a half . To the first 10 When I want to eat again in the morning , See there's only one peach left . Ask how much you picked on the first day .

1. Program analysis : Adopt the method of converse thinking , Infer from back to front .

public class Monkeys eat peaches {

static int total(int day){

if(day == 10){

return 1;

}

else{

return (total(day+1)+1)*2;

}

}

public static void main(String[] args)

{

System.out.println(total(1));

}

}

【 Program 17】 subject : Two ping-pong teams play , Three people each . Team a is for a,b,c Three people , Team B is x,y,z Three people . The match list has been drawn . Someone asked the players for the list of the game .a Say he's not at peace x Than ,c Say he's not at peace x,z Than , Please program to find out the players of the three teams .

1. Program analysis : The way to judge prime numbers : Remove with a number 2 To sqrt( The number of ), If it can be divided , It means that this number is not a prime number , On the contrary, prime numbers .

import java.util.ArrayList;

public class pingpang {

String a,b,c;

public static void main(String[] args) {

String[] op = { “x”, “y”, “z” };

ArrayList arrayList=new ArrayList();

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int k = 0; k < 3; k++) {

pingpang a=new pingpang(op[i],op[j],op[k]);

if(!a.a.equals(a.b)&&!a.b.equals(a.c)&&!a.a.equals(“x”)

&&!a.c.equals(“x”)&&!a.c.equals(“z”)){

arrayList.add(a);

}

}

for(Object a:arrayList){

System.out.println(a);

}

copyright:author[m0_ sixty-four million eight hundred and sixty-seven thousand a],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261552188852.html