java job assembly (loop nesting)

One / backward calculation of the
monkey eating peach problem: the monkey picked a number of peaches on the first day, and immediately ate half of them, not addicted, and ate another one and ate half of the remaining peach the next morning Eat one. After that, every morning I ate the remaining half of the previous day, zero. When I wanted to eat again on the morning of the 10th day, I saw only one peach left. Ask how many were picked on the first day. Procedural analysis: take the method of reverse thinking and infer from back to front
/

int day,x1, x2;
	 day=9 ; 
	 x2 =1;
	 while(day>0) {x1=(x2+1)*2; x2=x1; day--;}
	 System.out.println(x2);
	

Two / decompose prime factors Decompose prime factors
of a positive integer. For example: enter 90 and print 90 = 2
3 3 5.
Program analysis: To decompose prime factors for n, first find a smallest prime number k, and then complete the following steps:
(1) If this prime number is exactly equal to n, it means that the process of decomposing prime factors has ended, just print it out .
(2) If n <> k, but n is divisible by k, the value of k should be printed out, and the quotient of n divided by k should be used as the new positive integer n, and the first step should be repeated.
(3) If n is not divisible by k, then use k + 1 as the value of k and repeat the first step. * /

 System.out.println("请输入一个正整数");
     Scanner scanner=new Scanner(System.in);
     int n=scanner.nextInt();
     System.out.print(n+"=");
     for(int k=2; k<=n;k++) {
    	 while(n!=k) {
    		 if (n%k==0) {
    			 System.out.print(k+"*");
    			 n=n/k;}
    		 else {break;}
    			 
    		 }
    	 }
     System.out.print(n);

Three anti-index calculations
A ball falls freely from a height of 100 meters. After each landing, it jumps back to half of its original height. After falling again, how many meters did it pass when it landed for the 10th time?
How high is the 10th rebound? * /

float total=100;                   //总路程。
		float startHeight=100;           //每一次下落后弹起的高度
		                           //计算在第十次落地时已经走过的路程。(第十次已经落地但是并没有走第十次的路程)
		for(int i=1;i<=9;i++){
			startHeight=startHeight/2;                //计算此次弹起单程高度
			
			total=startHeight*2+total;                //将此次弹起并落地的高度加到总路程中。
		}
		
		System.out.println("totalmeters:"+total);
		System.out.println("第十次高度:"+startHeight/2);

Four complete numbers in
a specified range If a number is exactly equal to the sum of its factors, this number is called "final number".
For example, 6 = 1 + 2 + 3. Program to find all the counts within 1000. * /

for(int i=2;i<=1000;i++) 
	      {
		 int sum = 0; for(int j=1;j<i;j++) {if(i%j==0) {  sum+=j;   }   }     
		 if(sum==i)       
		 { System.out.print(i+ "    ");}  
      }

Five combinations
There are 1, 2, 3, 4 numbers, how many three-digit numbers can be composed of different and non-repeating numbers? How much are they?
Program analysis: The numbers that can be filled in the hundreds, tens, and ones are 1, 2, 3, and 4. Go after making up all the arrangements. Unsatisfied arrangement * /

int x=0, y=0, z=0, count=0;
		for(x=1;x<=4;x++) {
			for(y=1;y<=4;y++) {
				if(y==x) {continue;}
for(z=1;z<=4;z++) {if(y!=z&&z!=x) {System.out.println("组合的数分别别是"+x+y+z); count++;} }
			}
		}
	System.out.println("总共可以组合的数有"+count+"个");
Published 28 original articles · Like1 · Visits1708

Guess you like

Origin blog.csdn.net/qq_45870494/article/details/103333390