C#旅途 ----趁热打铁 活学活用 数组快排,查找,拷贝;

需要掌握的部分语法:

1.数组的定义:(这里和C不同的是,先括号,后变量名字)

<strong>(1)         int[] Inum;
             string[] Inum;2.</strong></strong>
<strong>(2)        int[] Inum = new int[10];</strong></strong>

<strong>(3)        int[] Inum; Inum = new int[10];
</strong>

2.输入(用for循环+convert类输入);

3.处理数组的类:Array类

   (1)sort:升序排序(PS:会把开范围的数组整体排序,所以会出错;所以按数组需要开)

   (2)IndexOf():查找首次出现元素位置;  LastIndexOf():查找最后一次元素出现位置;

   (3)Reverse:逆序

   (4)Copy:复制a数组到b数组;


(一)数组的快排

<strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n,i;
            
            Console.WriteLine("请输入数组大小:");
            n = Convert.ToInt32(Console.ReadLine());
            int[] num = new int[n];///*按需求开数组
            Console.WriteLine("请输入n个数字:");
            for(i=0;i<n;i++)
            {
                num[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("未排序时的数组为:");
            for(i=0;i<n;i++)
            {
                Console.Write(num[i] + " ");
            }
            Array.Sort(num);//*排序
            Console.WriteLine();
            Console.WriteLine("排序后的数组为:");
            for (i = 0; i < n; i++)
            {
                Console.Write(num[i] + " ");
            }
            Console.ReadKey();
        }
    }
}
</strong>


(二)数组元素位置的查询:

<strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 查找1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("请输入数组大小");
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n+1];
            Console.WriteLine("请输入"+n+"个数");
            for( i =1 ; i<=n;i++)
            {
                a[i]=Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("请输入需要查找的数");
            int b, c,search;
            search=Convert.ToInt32(Console.ReadLine());
            b = Array.IndexOf(a, search);
            Console.WriteLine("元素" + search + "首次出现的位置为:" + b);
            c = Array.LastIndexOf(a, search);
            Console.WriteLine("元素" + search + "最后一次出现的位置为:" + c);
            Console.ReadKey();
        }
    }
}
</strong>


(三)数组的逆序:PS(Reverse函数逆序后是从下标为0开始计数的)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 查找1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("请输入数组大小");
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n];
            Console.WriteLine("请输入"+n+"个数");
            for( i =0; i<n;i++)
            {
                a[i]=Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("未逆序的数组为:");
            for(i=0; i<n;i++)
            {
                Console.Write(a[i]+" ");
            }
            Console.WriteLine();
            Console.WriteLine("逆序后的数组为:");
            Array.Reverse(a);
            for(i=0;i<n;i++)
            {
                Console.Write(a[i]+" ");
            }
            Console.ReadKey();
        }
    }
}






  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kelisita

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值