Question Source: Http://student.csdn.net/mcs/programming_challenges?&page=1 Watch the World Cup
In the past in the school to participate in the annual ACM Program Design Contest, the feeling of the program algorithm is quite interesting, the two days found a website released some algorithm questions, a bit of that mood, see, feel able to drop, then kill it. At present, still in the company vigil, idle nothing to try the algorithm problem.
Quick hand hit, no detailed examination, there may be flaws please forgive me, but read the problem, solve the problem, algorithm design, a lot.
if (type = = 1) {...} This paragraph, although write a little complex, but is a simple optimization, hehe.
Before doing things, see first, then start. Program Ape time is limited, do not arbitrarily waste, wrong interpretation, will eventually result in an incorrect result.
classProgram {/* * The World Cup is on the hot, roommate at sleep time, early morning to watch, but Njzy is not very interested in this, he is thinking about a problem: * Assuming that the World Cup table has n seats, lined up in a row, visitors come here to occupy a place free. In general, a visitor first consider the seat * must be on both sides of the seat, the next consideration is the seat of no one, and finally no consideration, can only choose a seat on both sides is a person. * Assuming that there are n visitors occupying the place in turn, everyone chooses their seats according to the above rules, and Njzy is thinking about how many of the possible cis-orders of the N visitors can you help him? Input Description: There are several sets of test data, each set of test data including a positive integer n (0<n<1000000). Output * Description: For each set of data, because the answer can be very large, so the output: answer%1000000007 * * ************************************** * * First read the title carefully: * 1. There are n seats on the World Cup viewing table, and n seats are available to the visitors who come in turn. * Parse: Hide two points here (these two points are very important, if not read out these two points, the calculation will be very complex): * A. Number of seats equals number of visitors * B. The visitor has the order of entry (the middle of the problem The order of admission for guests is 1,2,3,4 ...) * (Restricted sorting algorithm) * * 2. The first consideration of the seat must be no one on either side * 3. The next consideration is the seat of no one on the side * * 4. You can only choose a seat on both sides. * * Output: * Indicates the visitor's seat selection method (the order number corresponds to the visitor's Entry order). * * * * Modify the value of Personindex to indicate the number of people entering the market (number of seats), for the title N (0<n<1000000) * Console.WriteLine (String) can be annotated if the method of total seat selection is required. Join (",", _sitearray)); * No output, improve the computational efficiency of the program.*/ Static voidMain (string[] args) { //assumption: Number of people, number of seats (...). Indicate the visitor's order of entry int_personcount =4; int[] _sitearray =New int[_personcount]; intPersonindex =1; intType =2; Switch(_sitearray.length) { Case 0: type =-1; Break; Case 1: type =0; Break; Case 2: type =1; Break; Case 3: type =2; Break; default: type =2; Break; } funcrun (ref_sitearray,refpersonindex, type); Console.WriteLine ("Total {0} method of selecting seats", Rescount); } Static voidSwap (ref intLref intr) {L= L ^R; R= L ^R; L= L ^R; } Static intRescount =0; Static voidFuncrun (ref int[] _sitearray,ref intPersonindex,inttype) { if(Personindex >_sitearray.length) {Console.WriteLine (string. Join (",", _sitearray)); Rescount++; return; } if(Type = =2) { //--------------------------------------------------- //first of all, the seats are definitely not on either side . for(inti =1; I < _sitearray.length-1; i++) { if(_sitearray[i] >0) Continue; if(_sitearray[i-1] ==0&& _sitearray[i +1] ==0) {_sitearray[i]= personindex++; Funcrun (ref_sitearray,refpersonindex, type); //Data Recoverypersonindex--; _sitearray[i]=0; }} Type--; } //--------------------------------------------------- //The next thing to consider is a seat with no one on the side if(Type = =1) { if(Personindex <=1|| _sitearray.length <3) return; if(_sitearray[0] ==0&& _sitearray[1] ==0) {_sitearray[0] = personindex++; Funcrun (ref_sitearray,refpersonindex, type); //Data Recoverypersonindex--; _sitearray[0] =0; } for(inti =1; I < _sitearray.length-1; i++) { if(_sitearray[i] >0) Continue; if(_sitearray[i-1] ==0) {_sitearray[i]= personindex++; Funcrun (ref_sitearray,refpersonindex, type); //Data Recoverypersonindex--; _sitearray[i]=0; } Else if(_sitearray[i +1] ==0) {_sitearray[i]= personindex++; Funcrun (ref_sitearray,refpersonindex, type); //Data Recoverypersonindex--; _sitearray[i]=0; } } if(_sitearray[_sitearray.length-1] ==0&& _sitearray[_sitearray.length-2] ==0) {_sitearray[_sitearray.length-1] = personindex++; Funcrun (ref_sitearray,refpersonindex, type); //Data Recoverypersonindex--; _sitearray[_sitearray.length-1] =0; } type--; } //--------------------------------------------------- //You can just pick a seat on both sides of the table . if(Type = =0) { for(inti =0; i < _sitearray.length; i++) { if(_sitearray[i] = =0) {_sitearray[i]= personindex++; Funcrun (ref_sitearray,refpersonindex, type); //Data Recoverypersonindex--; _sitearray[i]=0; } } } } }
College challenge: Watch the World Cup--limit sorting algorithm