The following are some common Java questions:
1.
Code:
Package cn.wwh.www.exe rcise; import Java. util. role;/*** role: 1. output N * n multiplication table *** @ author yiye banzhou * @ version 1.0 * @ Creation Time: september 4, 2014 6:09:08 */public class printmulttable {public static void main (string [] ARGs) {system. out. print ("Enter the number of rows:"); export SCN = new partition (system. in); int num = SCN. nextint (); printnum (Num);}/*** @ Param innum * the integer entered on the console **/Private Static void printnum (INT innum) {If (innum <1) {system. Out. println ("input error! "); Return;} system. out. println ("output"); For (INT I = 1; I <= innum; I ++) {for (Int J = 1; j <= I; j ++) {system. out. print (J + "*" + I + "=" + (I * j) + "\ t");} system. out. println ();}}}
2.
Package cn.wwh.www.exe rcise; import Java. util. role;/*** role: 2. based on the input start number and end number, output all the prime numbers in this range. *** @ author a leaf boat * @ version 1.0 * @ Creation Time: september 4, 2014 6:26:41 */public class printprime {public static void main (string [] ARGs) {system. out. print ("Enter start data:"); Country SCN = new partition (system. in); int beginnum = SCN. nextint (); SCN = new partition (system. in); system. out. print ("Enter end data:"); int endnum = SCN. nextint (); // Call the output function outprime (beginnum, endnum );} /*** @ Param beginnum * Start Number * @ Param endnum * end number */Private Static void outprime (INT beginnum, int endnum) {If (beginnum> = endnum) {system. out. println ("incorrect input range! ");} Int COUNT = 0; // used for wrapping int sum = 0; // sum of all prime numbers and for (INT I = beginnum; I <endnum; I ++) {If (isprime (I) {system. out. print (I + "\ t"); sum = I + sum; If (++ count % 8 = 0) system. out. println () ;}} system. out. println ("\ ncount:" + Count + "\ tsum:" + sum );} /*** @ Param num * integer to be judged * @ return returns true if I is a prime number; otherwise, false */Private Static Boolean isprime (INT num) is returned) {Boolean flag = false; If (Num <= 1) {return flag;} If (num = 2) {return true ;}for (INT I = 2; I <num; I ++) {If (Num % I = 0) {return flag;} if (I * I> = num) {flag = true ;}} return flag ;}}
3.
Code:
Package cn.wwh.www.exe rcise; import Java. util. role;/*** role: 3. output The number of daffodils * idea: for example, if you enter a three-digit number, the range of the three digits is [123,) * assume that the value is? * A> temp = 123 last = 123/1% 10 = 3 Sum = 3*3*3 * B> temp = 12 last = 12% 10 = 2 sum = sum + 2*2*2 * C> temp = 1 last = 1% 10 = 1 sum = sum + 1*1*1 *** @ author * @ version 1.0 * @ creation time: september 4, 2014 8:48:25 */public class printnarcissusnum {public static void main (string [] ARGs) {system. out. print ("Enter the number of digits:"); audio SCN = new digit (system. in); int numlength = SCN. nextint (); system. out. println (numlength + "the number of bits in the number of BITs:"); // the minimum number of bits in the input int begin = (INT) math. pow (10, numlength-1); For (INT I = begin; I <begin * 10; I ++) {If (isnarcissus (I, numlength) {system. out. print (I + "\ t") ;}}/ *** @ Param I * @ return returns true if the number of daffodils is used, otherwise, false */Private Static Boolean isnarcissus (INT num, int length) {int temp = 0; // The sum of all numbers in the record int sum = 0; // records the numbers in each bit, and records int last = 0 from the right to the left; For (INT I = 0; I <length; I ++) {int t = (INT) math. pow (10, I); temp = num/T; last = TEMP % 10; sum = (INT) (sum + math. pow (last, length);} return sum = num ;}}
4.
Note: The idea of the first method is to use an array to simulate a ring, but the biggest difficulty is the copying of numbers, that is, after the number is eliminated, the remaining number is made up of a ring.
The second method is to directly use a circular linked list, but the greatest difficulty is to format the output result according to the question.
Code:
Method 1:
Package cn.wwh.www.exe rcise. selectmonkey; import Java. util. arrays; import Java. util. role;/*** function: 5. monkey King election * this kind of practice has vulnerabilities and is not perfect (it is a mistake, please do not refer to it) * I think this idea is correct, but there are some problems with processing the branch, write down here, in the future, I will take a closer look at the ** @ author yiye banzhou * @ version 1.0 * @ creation time: september 5, 2014 12:32:15 */public class selectking1 {public static void main (string [] ARGs) {While (true) {system. out. print ("Enter the number of monkeys M:"); then SCN = new monkey (system. in); int Tal = SCN. nextint (); SCN = new partition (system. in); system. out. print ("Enter the number n (n <m):"); int outnum = SCN. nextint (); int King = getking (total, outnum); system. out. println ("Number" + King + "Monkey King! ");}} /*** @ Param total * Total Number of monkeys * @ Param outnum * number to be eliminated * @ return Monkey King Number */Private Static int getking (INT total, int outnum) {// The number returned by the record, int King = 0; int begin = total; int Circle = 1; Boolean flag = true; // initialize the array, the array length is total + 1, and a temporary array int [] Monkey = new int [Total + 1]; int [] temp = new int [Total + 1]; // make the data in the array equal to the subscript for (INT I = 0; I <total + 1; I ++) {monkey [I] = I ;}while (FLAG) {// There is still a monkey left. If (Total = 1) {King = monkey [1]; break;} // used for a flag, used to record the position of the maximum n multiple in an array int Sign = 0; if (total! = 1) system. out. print ("th" + circle + ","); // set the array position of this n multiple to 0, use sign to record the largest n multiple subscript for (INT I = 1; I <total + 1; I ++) {if (I % outnum = 0) {system. out. print ("no." + monkey [I] + "only monkey reported" + I + "quit! "); Monkey [I] = 0; Sign = I ;}} int num = 1; if (total> outnum) {// place the sing + 1-Total + 1 position for (INT I = Sign + 1; I <total + 1; I ++) {// if (I> = total + 1) break; If (monkey [I]! = 0) {temp [num ++] = monkey [I] ;}// copy data from a non-0 position to the temp array for (INT I = 1; I <sign; I ++) {If (monkey [I]! = 0) {temp [num ++] = monkey [I]; // num-1 monkeys left} // copy the data in the temp array to the monkey array. // first, set all monkeys to 0for (INT I = 1; I <total + 1; I ++) {monkey [I] = 0;} monkey = arrays. copyofrange (temp, 0, num); If (Num-1 = 1 | num = 1) {King = monkey [1]; flag = false;} system. out. println ("currently exited" + (total + 1-num) + "only monkey. "); // The remaining number of monkeys Total = num-1; // The number of returned circles (rounds) circle ++; If (Total = outnum) {system. out. print ("second" + circle + "Wheel report,"); system. out. print ("no." + monkey [outnum] + "only monkey reported" + outnum + "quit! "); System. Out. println (" one monkey is exited currently. "); Monkey [outnum] = 0; Total = total-1; Circle ++; Continue ;}} else {// the maximum flag bit is less than the number of outnumwhile (true) {// int leftnum = total; int loop = 0; int tempnum = 0; For (INT I = 1; I <begin * 2; I ++) {tempnum = total * I-outnum; loop = I; If (tempnum> 0) {break;} else if (tempnum = 0) {loop ++; break ;}} num = 1; for (INT I = 0; I <loop; I ++) {for (Int J = 1; j <total + 1; j ++) {temp [num ++] = monkey [J] ;}} if (Te Mpnum = 0) {system. out. print ("no." ++ circle + ",");} system. out. print ("no." + temp [outnum] + "only monkey reported" + outnum + "quit! "); System. Out. println (" one monkey is exited currently. "); Temp [outnum] = 0; // system. out. println (arrays. tostring (temp); // first, set all monkeys to 0for (INT I = 1; I <total + 1; I ++) {monkey [I] = 0 ;} if (tempnum = 0) {// system. out. println ("Num:" + num); num = num-1; monkey = arrays. copyofrange (temp, outnum, num);} else {monkey = arrays. copyofrange (temp, outnum, num);} system. out. println ("num-1-outnum:" + (Num-1-outnum); system. out. println ("Total" + total); In T m = monkey. length; system. Err. println (m); If (total-1! = 1 & total-1! = Num-1-outnum) {for (INT I = 1; I <temp. length; I ++) {monkey [M ++] = temp [I]; system. out. println (arrays. tostring (monkey); If (temp [I] = temp [outnum]) {break ;}} Total = total-1; // another monkey is left. If (Total = 1) {King = monkey [1]; break ;}}} return King ;}}
Method 2:
Package cn.wwh.www.exe rcise. selectmonkey;/*** class: The Role of a monkey entity class *** @ author @ * @ version 1.0 * @ Creation Time: september 5, 2014 10:43:15 */public class monkey {// monkey No. Public int no; // The current monkey's next monkey public monkey nextmonkey = NULL; Public monkey (INT No) {This. no = No ;}}
Package cn.wwh.www.exe rcise. selectmonkey;/*** class function: Build a monkey ring. *** @ author a boat * @ version 1.0 * @ Creation Time: september 5, 2014 10:46:46 */public class named elinked {// Number of monkeys in the circular circle private int num; // The first monkey, that is, 1 private monkey firstmonkey = NULL; private monkey tempmonkey = NULL; // create a number of monkey circles with num public incluelinked (INT num) {This. num = num; For (INT I = 0; I <num; I ++) {// if it is the first monkey if (I = 0) {// create a monkey = New monkey (I + 1); // point to the first monkey this. firstmonkey = monkey; this. tempmonkey = monkey;} else if (I = num-1) {// last monkey // create a monkey = new monkey (I + 1); this. tempmonkey. nextmonkey = monkey; this. tempmonkey = monkey; // The Last monkey points to the first monkey this. tempmonkey. nextmonkey = This. firstmonkey;} else {// create a monkey = new monkey (I + 1); this. tempmonkey. nextmonkey = monkey; this. tempmonkey = monkey ;}}}/** * @ Param outnum number to be eliminated **/Public void specak (INT outnum) {// number of remaining monkeys int last = This. num; // number of records in a circle int COUNT = 0; // number of records int Circle = 1; monkey first = This. firstmonkey; monkey pre = first; monkey Delete; monkey Prepre = first; system. out. print ("second" + circle + "Wheel report,"); While (last! = 1) {// under number n-1, locate the previous for (INT I = 1; I <outnum-1; I ++) {While (Prepre. nextmonkey. no! = Pre. no) {Prepre = Prepre. nextmonkey; If (Prepre. nextmonkey. no = pre. no) break;} // system. out. println ("pre. nextmonkey. no "+ pre. nextmonkey. no); // system. out. println ("pre. no "+ pre. no); // system. out. println ("Prepre. no "+ Prepre. no); If (pre. nextmonkey. no> pre. no & count! = 0 & Pre. No <Prepre. No) | (PRE. nextmonkey. No <PRE. No & count! = 0 & pre. no> Prepre. no) {system. out. println ("current exit" + Count + "only"); // The unit is in a loop. The next monkey report number is greater than the current monkey report number. Circle ++; system. out. print ("no." + circle + "report,"); Count = 0;} // current = pre. nextmonkey; Pre = pre. nextmonkey; // current = current. nextmonkey; while (Prepre. nextmonkey. no! = Pre. no) {Prepre = Prepre. nextmonkey; If (Prepre. nextmonkey. no = pre. no) break;} // system. out. println ("pre. nextmonkey. no "+ pre. nextmonkey. no); // system. out. println ("pre. no "+ pre. no); // system. out. println ("Prepre. no "+ Prepre. no); If (pre. nextmonkey. no> pre. no & count! = 0 & Pre. No <Prepre. No) | (PRE. nextmonkey. No <PRE. No & count! = 0 & pre. no> Prepre. no) {system. out. println ("current exit" + Count + "only"); // The unit is in a loop. The next monkey report number is greater than the current monkey report number. Circle ++; system. out. print ("cir" + circle +", "); Count = 0 ;}// Delete the next node = pre. nextmonkey; pre. nextmonkey = Delete. nextmonkey; Pre = pre. nextmonkey; system. out. print ("th" + Delete. no + "Monkey only reported" + outnum + "No. Quit! "); Count ++; while (Prepre. nextmonkey. No! = Pre. no) {Prepre = Prepre. nextmonkey; If (Prepre. nextmonkey. no = pre. no) break;} // system. out. println ("pre. nextmonkey. no "+ pre. nextmonkey. no); // system. out. println ("pre. no "+ pre. no); // system. out. println ("Prepre. no "+ Prepre. no); If (pre. nextmonkey. no> pre. no & count! = 0 & Pre. No <Prepre. No) | (PRE. nextmonkey. No <PRE. No & count! = 0 & pre. no> Prepre. no) {system. out. println ("current exit" + Count + "only"); // The unit is in a loop. The next monkey report number is greater than the current monkey report number. Circle ++; system. out. print ("cir" + circle +", "); Count = 0;} Last --; If (last = 1) {system. out. println ("currently exited" + Count + "only. ") ;}} System. Out. println (" no "+ pre. No +" the monkey is the Monkey King! ");} Public static void main (string [] ARGs) {// using elinked test = new using elinked (7); // test. specak (3); // monkey first = test. getfirstmonkey (); // monkey temp = first; // test whether the monkey circle is created correctly. // do {// system. out. println ("monkey report:" + temp. no); // temp = temp. nextmonkey; //} while (temp! = First );}}
Package cn.wwh.www.exe rcise. selectmonkey; import Java. util. role;/*** role: 2. using a circular linked list to deal with the Monkey King's question * the greatest difficulty is to format the output result (this is the result of analysis + Continuous testing) ** @ author yiye Binzhou * @ version 1.0 * @ Creation Time: 10:42:38, January 1, September 5, 2014 */public class selectking2 {public static void main (string [] ARGs) {While (true) {system. out. print ("Enter the number of monkeys M:"); then SCN = new monkey (system. in); int Total = SCN. nextint (); SCN = new partition (system. in); system. out. print ("Enter the number n (n <m):"); int outnum = SCN. nextint (); // create a total length monkey circle named elinked test = new incluelinked (total); // remove monkey, test. specak (outnum );}}}
J2se exercises (1)