zoukankan      html  css  js  c++  java
  • Array Reserve

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace SurfaceQuestion.Grammar
    {
        
    public class ArrayReverse
        {
            
    public ArrayReverse()
            {
                
    int[] a = new int[] { 123456 };
                
    this.Way1(a);
                Console.WriteLine(
    "_____________________");

                
    this.Way2(a);
                Console.WriteLine(
    "_____________________");

                
    this.Way3(a);
            }

            
    /// <summary>
            
    /// 新数组
            
    /// </summary>
            private void Way1(int[] a)
            {
                
    int[] b = new int[a.Length];
                
    for (int i = 0; i < a.Length; i++)
                {
                    Console.WriteLine(
    "count:"+i);
                    Console.Write(a[i]);
                    b[i] 
    = a[a.Length - 1 - i];
                }

                Console.WriteLine();
                
    foreach (var item in b)
                {
                    Console.Write(item);
                }
            }

            
    /// <summary>
            
    /// 临时变量
            
    /// </summary>
            
    /// <param name="a"></param>
            private void Way2(int[] a)
            {
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }

                
    int temp = 0;
                
    for (int i = 0; i < a.Length/2; i++)
                {
                    Console.WriteLine(
    "count:" + i);

                    temp 
    = a[i];
                    a[i] 
    = a[a.Length - 1 - i];
                    a[a.Length 
    - 1 - i] = temp;
                }

                Console.WriteLine();
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }
            }

            
    /// <summary>
            
    /// while
            
    /// mscorlib实现方式
            
    /// </summary>
            
    /// <param name="a"></param>
            private void Way3(int[] a)
            {
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }

                
    int num = 0;
                
    int num2 = a.Length - num - 1;
                
    while (num2 > num)
                {
                    Console.WriteLine(
    "count:" + num);

                    
    int temp = a[num];
                    a[num] 
    = a[num2];
                    a[num2] 
    = temp;
                    num
    ++;
                    num2
    --;
                }

                Console.WriteLine();
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }
            }
        }
    }


  • 相关阅读:
    AtCoder Regular Contest 093
    AtCoder Regular Contest 094
    G. Gangsters in Central City
    HGOI 20190711 题解
    HGOI20190710 题解
    HGOI 20190709 题解
    HGOI 20190708 题解
    HGOI20190707 题解
    HGOI20190706 题解
    HGOI 20190705 题解
  • 原文地址:https://www.cnblogs.com/chinaniit/p/1718871.html
Copyright © 2011-2022 走看看