Q & A of the Baidu STAR Program Design Competition (implemented in Java)-abnormal competition rules (unfinished !!!)

Source: Internet
Author: User

Sadly, it was not completely completed in the whole afternoon.


I have to admit that my IQ is very general. I had to figure out this integer division algorithm for nearly an hour. However, this abnormal question is not that simple !!!

I had to stand on the shoulders of my predecessors.

One of them is the big bull's: Click to open

One of them is the big bull's: Click to open

Thank you two. Unfortunately, I am not easy to learn, and I have not made it myself !!!

It's not too early. If you have time, try again and paste the code first. Please kindly advise me...

1. Create a bean class to store the number of members and the number of possible Competitions

/*** APR 2, 2013 * copyright (c) jackwang * all rights reserve * @ author <a href = "mailto: wangchengjack@163.com "> jackwang </a> */package COM. example. baidupro;/*** used to store N and K value * @ author Wangcheng **/public class inputdata {private string N; private string K; Public String getn () {return N ;} public void setn (string N) {n = N;} Public String getk () {return K;} public void setk (string K) {k = K ;} /*** @ Param N * @ Param K */Public inputdata (string N, string K) {super (); n = N; k = K ;}}

2. Business Code:

/*** APR 2, 2013 * copyright (c) jackwang * all rights reserve * @ author <a href = "mailto: wangchengjack@163.com "> jackwang </a> */package COM. example. baidupro; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. util. arraylist; import Java. util. iterator; import Java. util. list;/*** unusual competition rules in order to promote communication between employees in various departments, Baidu (Baidu) held a "boxing match" across the company ", * The Champion of Baidu is responsible for organizing the competition. "Fans W. Z. W. Z do not want to use the traditional knockout or round-robin * method, but they have developed their own rules. Some employees (such as employees in the same department or department) have many opportunities to interact with each other. To facilitate communication between different departments, * W. Z wants employees to form different groups. Every two people in different groups will have a friendship match, while those in the same group will not play any matches. For example, if four people are numbered 1-4, * if they are divided into two groups and one group is 1, 2, 3, and 4, a total of four matches are required: 1 vs 3, 1 vs 4, 2 vs 3 vs 4. if it is * 1, 2, 3, 4, and 4, then a total of three matches are required: 1 vs 4, 2 vs 4, 3 vs 4. soon W. z * realizes that such a competition rule may make the number of matches very large. W. Z want to know if there are n people, through the above rules, the total number of matches may be K? For example, if three people are assigned only to one group, there is no need to compete. If they are assigned to two groups, two matches are required. If they are divided into three groups, three matches are required. However, no matter how you score, it is impossible to have only one game. * I believe that as a programmer, you must know how to answer this question? Now, please help W. Z. Each input row is a group of data, which contains two numbers, N and K. * (0 <n <= 500, k> = 0) Outputs n to the input. K. If n employees use a certain grouping method, they may need a total of K games, "Yes" is output; otherwise, * "no" is output. Each group of data occupies one row. All inputs and outputs are standard inputs and outputs. Example input file: 2 0 2 1 3 1 3 2 output: yes No Yes ** @ author Wangcheng **/public class boxinggamepro {Private Static list <inputdata> input = new arraylist <inputdata> (); // stores the number of input groups, because the initial size is Private Static list <integer> waylist = new arraylist <integer> (); Private Static int K; Private Static Boolean isfind = false;/*** all exceptions are thrown, temporarily not process ** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {// 1, read the input data getinput (); // 2, read computeappropriate ();} public static void computeappropriate () {// The Key to the idea of integer division is to calculate the number of matches in each grouping method, therefore, each method must be put in a linked list. // iterator <inputdata> it = input. iterator (); While (it. hasnext () {inputdata OBJ = it. next (); string key = obj. getn (); string value = obj. getk (); k = integer. parseint (value); // system. out. println (Key + ":" + value); int n = integer. parseint (Key); search (n, n); If (! Isfind) {system. out. println ("no") ;}} public static void search (int n, int N2) {If (n2 = 0) {// if you place all personnel in a group waylist. add (n); Judge (waylist); waylist. clear () ;}if (n2 = 1) {for (INT I = 0; I <n; I ++) {waylist. add (1);} // system. out. println (waylist); Judge (waylist); waylist. clear () ;}else {for (INT I = n2; I> 1; I --) {// 1 + f (m-1); (n = m) if (isfind) {break;} If (n = n2) {waylist. add (1); Judge (waylist); waylist. clear ();} else {W Aylist. Add (I); search (n-I, (I <n-I )? I: (n-I); // It's down ..... Waylist. clear () ;}}} public static void judge (list <integer> waylist2) {int value = 0; integer [] In = new integer [waylist2.size ()]; in = (integer []) waylist2.toarray (in); For (integer INTEGER: In) {system. out. print (integer + "");} system. out. println (); For (INT I = 0; I <in. length; I ++) {If (in. length = 1) {value = 0; continue;} For (Int J = I + 1; j <in. length; j ++) {value + = in [I] + in [J]; // system. out. println (value );}} If (value = k) {system. out. println ("yes"); isfind = true ;}} public static void getinput () throws ioexception {system. out. println ("Enter data. Separate data pairs by spaces (enter over to end the input !!!)... "); // Cannot be determined, so two-dimensional arrays cannot be used. Because there is a ing between data pairs, hashmapbufferedreader bufr = new bufferedreader (New inputstreamreader (system. in); string line = NULL; while (line = bufr. readline ())! = NULL) {If ("over ". equals (line) {break;} // 1.1 format for processing input data string [] STR = line. split ("+"); // can be processed even if the user inputs multiple spaces at a time; If (Str. length! = 2) {system. Out. println ("the input data is incorrect. Please enter it again! "); System. Exit (0);} input. Add (New inputdata (STR [0], STR [1]) ;}}

I have been busy for more than an hour since I came back from class at tonight. I still haven't figured it out !!!

I think it's my basic question! I may not be able to pick up this peach for the moment, so I need to fill in the energy and have the ability to pick it again!

From tomorrow, we will move to the basics! Lay down the foundation first! This issue will be resolved later !!!

Hope you can give me some advice!

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.