Challenge programming Programming Competition Training Manual-1.6.8
Vote in Australia(Semantic Alian voting) http://blog.csdn.net/o0tt0o/article/details/8533155 supplement
Public class problem_australian_voting {/** 1. slight bug-if all votes are the same, no * 2 is displayed. the more data there is, the better the gap. The normal voting result is different. * 3. recursive loop is used. return is not allowed when a winner is encountered. The while clause is used to judge * 4. the name and number of votes of this question are not fixed, and the program cannot be written to death. It can only be expanded according to the N in the first line entered * 5. exercise caution when associating the names, total number of votes, and number of votes * 6. there are many variables for this question. There are many output row detection variables * @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubstring [] Vote = {"3", "John Doe", "Jane Smith", "Jane Austen", "1 2 3 ", "2 1 3", "2 3 1", "1 2 3", "3 1 2"}; string thewinner = prepare (vote); system. out. println (thewinner);} Private Static string prepare (string [] Vote) {// todo auto-generated method stubboolean flag = true; string Winner = "goon"; while (FLAG) {int member; int highvote; member = integer. parseint (vote [0]); // the first digit in the string group is the number of candidates int votearray [] = new int [member]; // define an array with the length equal to the number of candidates string name [] = new string [member]; // create a string group and save the candidate for (INT I = 0; I <name. length; I ++) {// read the candidate name [I] = vote [I + 1]; // Save the candidate // system. out. println (name [I]); test cycle} For (Int J = Member + 1; j <vote. length; j ++) {// read all voting information // system. out. println (vote [J]); // test the loop for (INT I = 1; I <= member; I ++) {// subscripts of the loop array, that is, the corresponding candidate if (integer. parseint (string. valueof (vote [J]. tochararray () [0]) = I) {// if the first digit is equal to the candidate subscript, the vote for this person is + 1 votearray [I-1] ++; // system. out. println (I-1 + "" + votearray [I-1]); }}int [] backuparay = new int [votearray. length]; // back up system for array. arraycopy (votearray, 0, backuparay, 0, votearray. length); highvote = ordervote (votearray); // sort the votes and return the highest number of votes. // system. out. println (highvote); // system. out. println (vote. length-member-1)/2); // The system truncates the decimal number. Therefore, + 1 indicates more than half of the votes if (highvote> (vote. length-member-1)/2) {// if the maximum number of intent votes is greater than 50%, this person wins for (INT I = 0; I <backuparay. length; I ++) {// system. out. println (backuparay [I]); // system. out. println (highvote) = integer. parseint (string. valueof (backuparay [I]); If (highvote) = integer. parseint (string. valueof (backuparay [I]) {// system. out. println (I); Winner = Name [I]; flag = false; // system. out. println (name [I]);} // system. out. println (backuparay [I]) ;}} else {// if the maximum number of votes is less than half, int lownum = 0 is the candidate with the least first intention; // defines the minimum number of votes. The candidate number is int [] backuparrayagain = new int [backuparay. length]; // back up system for array. arraycopy (backuparay, 0, backuparrayagain, 0, backuparay. length);/* Find the candidate with the least first intention */INT lowvote = lowvote (backuparay); // find the lowest ticket for (INT I = 0; I <backuparrayagain. length; I ++) {If (backuparrayagain [I] = lowvote) {lownum = I ;}// system. out. println (lownum + 1); For (INT I = Member + 1; I <vote. length; I ++) {// truncates the number of votes selected by the first intention from the first number and space (more than one minimum) // system. out. println (vote [I]. tochararray () [0]); If (integer. valueof (string. valueof (vote [I]. tochararray () [0]) = (lownum + 1) {vote [I] = vote [I]. substring (2, vote [I]. length (); // prepare (vote); // pass in new ballot information, recursive prepare function // until the maximum number of intent votes is greater than 50% of the selected votes. // system. out. println (vote [I]) ;}}} return winner;} Private Static int lowvote (INT [] backuparay) {// todo auto-generated method stubfor (INT I = 0; I <backuparay. length; I ++) {// find the highest first intention // system. out. println (votearray [I]); // bubble sort method. After sorting, backuparay [0] is the lowest number of votes for (Int J = I; j <backuparay. length; j ++) {If (backuparay [I]> backuparay [J]) {// int temp; temp = backuparay [I]; backuparay [I] = backuparay [J]; backuparay [J] = temp ;}}return backuparay [0];} Private Static int ordervote (INT [] votearray) {// todo auto-generated method stubfor (INT I = 0; I <votearray. length; I ++) {// find the highest first intention // system. out. println (votearray [I]); // bubble sort method. After sorting, votearray [0] is the highest number of votes for (Int J = I; j <votearray. length; j ++) {If (votearray [I] <votearray [J]) {// sort int temp from large to small; temp = votearray [I]; votearray [I] = votearray [J]; votearray [J] = temp ;}} return votearray [0] ;}}