• 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);
                }
            }
        }
    }


  • 相关阅读:
    JSK42586.Tree(动态开点线段树+树上启发式合并)
    CF1188B.Count Pairsl(数学)
    2021ICPC亚洲区域赛(昆明)复盘
    1009F.Dominant Indices (树上启发式合并)
    246E.Bloods Counsin Return(离线+树上启发式合并)
    208E.Blood Cousins(离线+倍增LCA+树上启发式合并)
    208E.Blood Cousins (离线+树上启发式合并)
    570D.Tree Requests (离线+树上启发式合并)
    Java从入门到实战之(6)SSM框架使用
    Java从入门到实战之(5)Java集合对比汇总
  • 原文地址:https://www.cnblogs.com/chinaniit/p/1718871.html
Copyright © 2020-2023  润新知