数组练习题(2018/6/26)

1.创建一个字符数组,2行13列,保存26个字母

using System;

public class Test
{
    public static void Main()
    {
        //创建一个字符数组,2行13列,保存26个字母
        char[,] arrayChar = new char[2,13];

        arrayChar[0,0] = 'a';

        for(var i = 0;i < arrayChar.GetLength(0);i++){
            for(var j = 0;j <  arrayChar.GetLength(1);j++){
                arrayChar[i,j] = (char)(arrayChar[0,0] + i*13 + j);
            }
        }
        foreach(var item in arrayChar){
            Console.Write(item + " ");
        }
    }
}

2.把数组中能被3整除的数乘以4除以5用模代替

using System;

public class Test
{
    public static void Main()
    {
        //把数组中能被3整除的数乘以4除以5用模代替;
        int[,] arr={
            {1,2,3,4,5},
            {5,4,3,2,1},
            {2,3,2,4,6}
        };
        for(var i = 0;i < arr.GetLength(0);i++){
            for(var j = 0;j <  arr.GetLength(1);j++){
                if(arr[i,j]%3==0){
                    arr[i,j] = arr[i,j]*4%5;
                }
            }
        }

        foreach(var item in arr){
            Console.Write(item + " ");
        }
    }
}

3.{{1} ,{1,1},{1,1,1},{1,1,1,1}}
{{1},{1,2},{1,2,3},{1,2,3,4}}
{{1},{2,2},{3,3,3},{4,4,4,4}}
{{‘a’},{‘b’,’b’},{‘c’,’c’,’c’}}

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //{{1} ,{1,1},{1,1,1},{1,1,1,1}}
            int[][] arr = new int[4][];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = new int[i + 1];
                for (int j = 0; j < arr[i].Length; j++)
                {
                    arr[i][j] = 1;
                }
            }

            //{{1},{1,2},{1,2,3},{1,2,3,4}}
            int[][] arr1 = new int[4][];
            for (int i = 0; i < arr1.Length; i++)
            {
                arr1[i] = new int[i + 1];
                for (int j = 0; j < arr1[i].Length; j++)
                {
                    arr1[i][j] = j + 1;
                }
            }

            //{{1},{2,2},{3,3,3},{4,4,4,4}}
            int[][] arr2 = new int[4][];
            for (int i = 0; i < arr2.Length; i++)
            {
                arr2[i] = new int[i + 1];
                for (int j = 0; j < arr2[i].Length; j++)
                {
                    arr2[i][j] = i + 1;
                }
            }
            //{{'a'},{'b','b'},{'c','c','c'}}
            char[][] arr3 = new char[4][];
            for (int i = 0; i < arr2.Length; i++)
            {
                arr3[i] = new char[i + 1];
                for (int j = 0; j < arr3[i].Length; j++)
                {
                    arr3[i][j] = (char)(i + 'a');
                }
            }

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

4.杨辉三角

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //杨辉三角
            int n = Int32.Parse(Console.ReadLine());

            int[][] arr = new int[n][];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = new int[i + 1];
                for (int j = 0; j < arr[i].Length; j++)
                {
                    if(j ==0 || j == arr[i].Length - 1)
                    {
                        arr[i][j] = 1;
                    }
                    else
                    {
                        arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
                    }
                }
            }


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

5.数组(矩阵反转)定义一个二维数组a[3][4],输入整数到数组中,
然后将a数组矩阵反转后存入到b[4][3]数组中,并将b输出。
int[,] arr={{3,5,1,4},{7,2,6,8},{0,9,4,6}}
比如:
输入:
3 5 1 4
7 2 6 8
0 9 4 6
输出:
3 7 0
5 2 9
1 6 4
4 8 6

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.数组(矩阵反转)定义一个二维数组a[3][4],输入整数到数组中,
            //然后将a数组矩阵反转后存入到b[4][3]数组中,并将b输出。
            int[,] arr = { { 3, 5, 1, 4 }, { 7, 2, 6, 8 }, { 0, 9, 4, 6 } };

            int[,] newArr = new int[arr.GetLength(1), arr.GetLength(0)];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    newArr[j, i] = arr[i, j];
                }
            }

            Console.WriteLine("打印旧矩阵:");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("打印新矩阵:");
            for (int i = 0; i < newArr.GetLength(0); i++)
            {
                for (int j = 0; j < newArr.GetLength(1); j++)
                {
                    Console.Write(newArr[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

2.将一个int数组向左滚动平移1次。{3,5,8,7}→{5,8,7,3}

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //2.将一个int数组向左滚动平移1次。{ 3,5,8,7}→{ 5,8,7,3}
            int[] arr = { 3, 5, 8, 7 };
            int n = Int32.Parse(Console.ReadLine()); //滚动的次数
            Console.Write("滚动前的数组:");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }

            for (int x = 0; x < n; x++)
            {
                int temp = arr[0];
                for (int i = 0; i < arr.Length; i++)
                {
                    if (i != arr.Length - 1)
                    {
                        arr[i] = arr[i + 1];
                    }
                    else
                    {
                        arr[i] = temp;
                    }
                }
            }
            Console.WriteLine();
            Console.Write("向左滚动{0}次后的数组:",n);
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34937637/article/details/80812557