current position:Home>Classic java programming 50 questions

Classic java programming 50 questions

2022-01-26 23:00:19 #HashMap#

These topics are recommended by Lao Xue , Suitable for learning Java Programmers who don't know their mastery of basic grammar , These questions can exercise our thinking

[ Procedure 1 ] Classical questions : There is a pair of rabbits , From the day after birth 3 A couple of rabbits are born every month from , Every month after the third month, a couple of rabbits will be born , If the rabbits don't die , What is the number of rabbits per month ? Program analysis : First of all, we can write down the logarithm of rabbits in the previous few days on the draft paper , Then observe whether there is

Write down the logarithm of rabbits in the first few months 1,1,2,3,5,8,13…
It is found that its law is the same as that of high school series an=an-1+an-2; So it is very natural to write its algorithm

public class RabbitsNumber{
    
	public static void main(String[] args){
    
		for(;true;){
    // On the basis of the original, a cycle is added , The program does not exit after one input 
			// Show whether you go in ,exit sign out 
			System.out.print("Please make your choose(enter or exit):");
			Scanner scanner = new Scanner(System.in);// establish scanner object 
			String str = "";
			if(!str.equal("exit")){
    // Judge if the user enters exit Then exit the program 
				System.out.print("Please input month:");
				Scanner scanner = new Scanner(System.in);
				int month = scanner.nextLine();
				System.out.print("The number is:"+fun(month));// Call function recursively 
			}else{
    
				break;	
			}
		}
	}
	private static int fun(int month){
    // Recursive function 
		if(month==1||month==2){
    // Recursive header , Month is 1 and 2 Return at month 1
			return 1;
		}else{
    
			return fun(month-1)+fun(month-2);// Recursive body , Recursively call 
		}
	}
}

The operation results are as follows :
 Insert picture description here

[ Program two ] subject : Judge 101-200 How many primes are there between , And output all prime numbers .
public class PrimNum{
    
	public static voi main(String[] args){
    
		int count = 0;// Counter 
		for(int i = 101;i<=200;i++){
    
			if(IfPrimNum(i)){
    
				System.out.println(i);
				count++;
				if(count%6==0){
    // Every time 6 A new line 
					System.out.println();	
				}
			}
		}
	}
	private boolean IfPrimNum(int i){
    
		for(int j = 2;j<i;j++){
    
			if(i%j==0){
    // If it's not prime , Then return to false
				return false;
			}
		}
		return true;
	}
}

The operation results are as follows :
 Insert picture description here

[ Procedure three ] subject : Print out 100-999 be-all " Narcissistic number ", So-called " Narcissistic number " A three digit number , The sum of its cubes is equal to the number itself . for example :153 It's a " Narcissistic number ", because 153=1 The third power of +5 The third power of +3 The third power of .

Program analysis : utilize for Cycle control 100-999 Number , Each number is decomposed into bits , ten , Hundred bit .

public class Daffodils{
    
	public static void main(String[] args){
    
		selectFlower();// Calling method 
	}
	public static void selectFlower(){
    
		int count = 0;// Record the total number of daffodils 
		System.out.print(" The number of daffodils is :");
		for(int num=100;num<=999;num++){
    
			int one = num%10;// Store single digits 
			int temp = num/10;// Store the first two 
			int two = temp%10;// Store ten digits 
			int three = temp/10;// Store hundreds 
			if(one*one*one+two*two*two+three*three*three == num){
    
				count++;// Add one... At a time 
				System.out.print(num+"\t");
			}
			if(num==999){
    // When num=999 To exit the loop 
				break;
			}
		}
		System.out.println(" The total number of daffodils :"+count);	// Displays the total number of daffodils 
	}
}

copyright notice
author[#HashMap#],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262300151212.html

Random recommended