Java pen questions, frequently encountered questions, with answers

Source: Internet
Author: User

/* Algorithm: the father and child are 30 years old, and the son is 6 years old, ask how many years after the father's age is 2 times the son's age */<br/> int BA = 30; <br/> int ER = 6; <br/> for (INT I = 0; I <100; I ++) <br/>{< br/> If (BA + I) = 2 * (ER + I) <br/>{< br/> system. out. println (I); <br/>}< br/>/* algorithm: write a program. The two arrays have 6 integers, merge into an array in ascending order (the length of the array is 12) */<br/> int [] A = new int [] {1, 2, 4, 12, 32, 10 }; <br/> int [] B = new int [] {34, 31, 54, 87, 11, 22 }; </P> <p> int [] C = new in T [12]; <br/> for (INT I = 0; I <. length; I ++) <br/>{< br/> C [I] = A [I]; <br/> C [I + 6] = B [I]; <br/>}< br/> arrays. sort (c); <br/> for (INT I = 0; I <C. length; I ++) <br/>{< br/> system. out. println (C [I]); <br/>}< br/>/* after a ball falls freely from the height of 100, it returns to half of the original height. According to this rule, how many meters does it take to reach the ground 10th times? How high will it rebound? */<Br/> float hight = 100; <br/> int n = 1; <br/> float sum = 0; <br/> for (n = 0; n <10; n ++) <br/>{< br/> sum = hight + sum; <br/> hight = hight/2; <br/>}< br/> system. out. println ("nth" + N + "times" + "total trip" + sum + "meters" + "rebound" + hight <br/> + "meters "); <br/>/* change coins: How many ways can I change one RMB to a coin of 5, 2, or 1? */<Br/> int num = 0; <br/> for (INT I = 1; I <= 20; I ++) // if all are 2 points, up to 50 <br/>{< br/> for (Int J = 1; j <= 50; j ++) // if all are 5 points, up to 20 <br/>{< br/> for (int K = 1; k <= 100; k ++) // if all are 1 point, up to 100 <br/> {<br/> If (5 * I + 2 * j + 1 * k = 100) <br/>{< br/> num = num + 1; <br/>}< br/> system. out. print (Num + ""); <br/>/** cattle breeding problem: a scientist once had a math problem: a cow, it generates a calf at the beginning of each year. <br/> * Starting from the fourth year, each heifer also produces a heifer at the beginning of each year. According to this rule, if no cow dies, how many cows are there in 20th years? */<Br/> int [] cownumber = new int [21]; <br/> cownumber [0] = 1; <br/> for (INT currentyear = 1; currentyear <= 20; currentyear ++) <br/> {<br/> If (currentyear <4) // the first three years, only old cows breed <br/>{< br/> cownumber [currentyear] = cownumber [currentyear-1] + 1; <br/>}< br/> else <br/> // old cows breed also <br/> {<br/> cownumber [currentyear] = cownumber [currentyear-1] <br/> + cownumber [currentyear-4 + 1]; <br />}< Br/> system. out. println ("no." + currentyear + "year" + cownumber [currentyear]); <br/>}< br/>/** print the number of all divisible by 7 or 9 in 500 */<br/> int n1 = 0; <br/> for (INT I = 0; I <= 500; I ++) <br/> {<br/> If (I % 7 = 0) | (I % 9 = 0) <br/>{< br/> system. out. print (I + ""); <br/> N1 ++; <br/> system. out. println (); <br/>}< br/> system. out. println (N1); <br/>/** <br/> * write a Java program to generate a year between 1800 and 2000., Print whether it is a leap year. The leap year is the year after January 1, 1584. It must be divisible by 400. <br/> * It must be divisible by four but not by 100, math is known to be used. the random () method can generate a random decimal number between 0 and 1 .) <br/> */<br/> long year = (math. round (math. random () * 200) + 1800); <br/> system. out. println (year); <br/> If (Year % 400 = 0) | (Year % 4 = 0 & year % 100! = 0) <br/>{< br/> system. Out. println (Year + "is the latest year! "); <Br/>}< br/> else <br/>{< br/> system. Out. println (Year +" is a year! "); <Br/>}< br/>/** write a program to implement the calculation of 1 + 2 + 3 + n (n = 200) recursively. */<br/> // system. out. print (numbers (200); <br/> // The following is a custom method <br/> // public static int numbers (int n) <br/> // {<br/> // return n <= 1? 1: N + numbers (n-1); <br/> //} <br/>/***/<br/>/** specify a number, enter a number on the keyboard to find the number closest to the preset number. <br/> * The number indicates that it is the number entered by the keyboard. <Br/> */<br/> specify input = new partition (system. in); <br/> system. out. print ("Please input numbers: separated by commas:"); <br/> string STR = input. next (); <br/> string [] item = Str. split (","); <br/> int number = 80; <br/> int place = 0; <br/> int temp = 5000; <br/> int x = 0; <br/> for (INT I = 0; I <item. length; I ++) <br/>{< br/> system. out. println (item [I]); <br/> If (integer. parseint (item [I])> Number) <Br/>{< br/> X = integer. parseint (item [I])-number; <br/>}< br/> else <br/>{< br/> X = Number-integer. parseint (item [I]); <br/>}< br/> If (x <temp) <br/>{< br/> temp = X; <br/> place = I; <br/>}< br/> system. out. println ("the nearest number is:" + item [place] + "; appears in place + (place + 1) <br/> +" bit "); <br/>/** use Java, long. math class, generates 10 random integers between 0 and 99, and finds the minimum and maximum values */<br/> int [] num1 = new int [10]; <br/> F Or (INT I = 0; I <10; I ++) <br/> {<br/> num1 [I] = (INT) (math. random () * 99); <br/> system. out. print (num1 [I] + "/N"); <br/>}< br/> arrays. sort (num1); <br/> system. out. println ("minimum value:" + num1 [1]); <br/> system. out. println ("maximum value:" + num1 [9]); </P> <p>/** assume that a piece of steel is 2000 m long and takes half of it every day, write a program to find the number of days after the steel length is less than 5 meters. */<br/> double num11 = 2000; <br/> int day = 0; <br/> for (INT I = 0; I <10000; I ++) <br/>{< br/> num 11 = num11/2; <br/> system. out. println (num11); <br/> day ++; <br/> If (num11 <5) <br/>{< br/> break; <br/>}< br/> system. out. println ("day +" day, the steel length is less than 5 meters! "); </P> <p>/** the sum of all its factors (including 1 but not including the number itself) is equal to or equal to this number, for example, 28 = 1 + 2 + + 4 + 7 + 14, and 28 is a full number. <br/> * write a program to obtain all the complete numbers between 2 and 100. */<br/> int m = 0; <br/> for (INT N11 = 2; N11 <= 100; N11 ++) <br/> {<br/> m = 0; <br/> for (INT I = 1; I <N11; I ++) <br/>{< br/> If (N11% I = 0) <br/>{< br/> M + = I; <br/> If (M = N11) <br/>{< br/> system. out. println (N11 + "Full count"); <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.