zoukankan      html  css  js  c++  java
  • 1. 与数组有关的题目

    1、按一行n个数输出数组

                for (int j = 0; j < array.Length; j++)
                {
                    Console.Write("{0}	",array[j]);
                    //当n=5的时候,打印到第4个元素的时候,要换行
                    // (4 + 1)% 5 = 0
                    if ((j + 1) % n == 0)
                    {
                        Console.WriteLine();
                    }
                }

    2、对数组进行冒泡排序

    //请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
            static int[] BoubleSort(int[] arr) 
            {
                bool isSwaped = false;
                for (int i = 0; i < arr.Length-1; i++)
                {
                    for (int j = 0; j < arr.Length - i - 1; j++)
                    {
                        if (arr[j] > arr[j+1])
                        {
                            int temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                            isSwaped = true;
                        }
                    }
                    if (isSwaped == false)
                    {
                        break;
                    }
                }
                return arr;
            }

    3、去除两个数组中的重复元素,然后再合并成一个数组

      可以先将数组转换成集合,待完成去重复元素、合并的操作后,再将集合转换成数组。

                List<string> list1 = new List<string>();
                List<string> list2 = new List<string>();
                list1.AddRange(new string[] { "a", "b", "c", "d", "e" });
                list2.AddRange(new string[] { "d", "e", "f", "g", "h" });
    
                //调用contains方法检测某个元素是否存在另一个集合中
                //是,则在另一个集合中删除这个元素
                foreach (var item in list1)
                {
                    if (list2.Contains(item))
                    {
                        list2.Remove(item);
                    }
                }
    
                //int[] arr = {1,2,3,4,5,6};
                //if (arr.Contains(6)) 
                //{
                //    Console.WriteLine(true);
                //}
    
                list1.AddRange(list2);
                foreach (var item in list1)
                {
                    Console.WriteLine(item);
                }
                string[] array = list1.ToArray();
                Console.ReadKey();

      

  • 相关阅读:
    I'm Telling the Truth
    B-shaass and lights codeForces
    1
    不容易系列之(4)——考新郎 HDU
    犯人冲突
    不互质的和
    OI回忆录
    NOI2018退役记
    uoj221【NOI2016】循环之美
    uoj220【NOI2016】网格
  • 原文地址:https://www.cnblogs.com/lcxBlog/p/4896119.html
Copyright © 2011-2022 走看看