The title is as follows: An array to invert the result, without using the reverse method.
Interview did not have, then a little nervous, know is related to the high School Knowledge series. Later on the internet and a bad fix, also found on the Internet a solution. First put down the solution on the Internet.
classProgram {Static voidMain (string[] args) { int[] ts=New[] {1,2,3,4,5,6,7}; int[] Ts2 =New int[TS. Length]; for(vari =0; I < TS. Length; i++) {Ts2[i]=ts[ts. length-i-1]; } for(vari =0; I < TS. Length; i++) {Console.WriteLine ("value:{0}", Ts2[i]); Console.WriteLine (","); }}}view CodeView Code
The code was simple and concise, but I felt a little bit smart in it, because he was a roundabout. As far as my character is, it is straightforward to do things, but it is no longer a roundabout tactic. It may be the reason of character. So I thought I'd think of a way to do this in the loop just to be elegant and intelligent. (not self-made, but the code elegant different definition just, oh, do not shoot bricks) first to see the law, the array length is 0,1,2,3,4,5,6,7,8,9 .... The corresponding loop is 0,0,1,1,2,2,3,3,4,4 ... The second observation is that the number of cycles of odd and even is the same. So we'll think about the for odd pages formula for high school. ( -1) ^n-1, and then observe, you will find that the number of cycles is the length of the even-numbered divided by 2, that is, len/2. So the odd numbers are divided into even 2. Odd even, usually odd-1, and then, try to get the formula for (n+ (( -1) ^n-1)/2)/2 so that 1 generation into the 0,2 into 1, this is what we are looking for. So the formula is simplified, 2n+ (( -1) ^n-1)/4, OK, here is the complete code.
classProgram {Static voidMain (string[] args) { int[] ts=New[] {1,2,3,4,5,6,7}; varn =ts. Length; varN1 = (2* n + (Math.pow (-1), N)-1)) /4; Console.WriteLine ("value:{0}\n\n", N1); for(vari =0; I < N1; i++) { inttemp; Temp=Ts[i]; Ts[i]= Ts[n-1-i]; Ts[n-1-I] =temp; } for(vari =0; I < n; i++) {Console.WriteLine ("value:{0}", Ts[i]); Console.WriteLine (","); } } }View Code
A question sharing (array reversal arrangement)