A question sharing (array reversal arrangement)

Source: Internet
Author: User

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 Code
View 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)

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.