首页 > 编程语言 > 详细

C#数组(EduCoder实训题目)

时间:2020-06-09 21:36:39      阅读:42      评论:0      收藏:0      [点我收藏+]

这才是今天的实训,没想到吧,哈哈哈!

第1关:逆序输出数组元素

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch701
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int n = Convert.ToInt32(Console.ReadLine());
            if (n < 0 || n > 10)
            {
                Console.WriteLine("input error!");
                return ;
            }
            int[] a = new int[n];
            for (int i = 0; i < n; ++i)
            {
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
 
            Array.Reverse(a);
            for (int i = 0; i < n; ++i)
            {
                Console.Write("{0} ", a[i]);
            }
            /*******end********/

        }
    }
}
View Code

第2关:数据排序

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch702
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int n = Convert.ToInt32(Console.ReadLine());
            if (n < 0 || n > 10)
            {
                Console.WriteLine("input error!");
                return ;
            }
            int[] a = new int[n];
            for (int i = 0; i < n; ++i)
            {
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
            Array.Sort(a);
            for (int i = n - 1; i >= 0; -- i)
            {
                Console.Write("{0} ", a[i]);
            }
            /*******end********/

        }
    }
}
View Code

第3关:求数组中的最大值和最小值

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch705
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            try
            {
                int n = Convert.ToInt32(Console.ReadLine());
                if (n < 0 || n > 10)
                {
                    Console.WriteLine("input error!");
                    return;
                }
                int[] a = new int[n];
                for (int i = 0; i < n; ++i)
                {
                    a[i] = Convert.ToInt32(Console.ReadLine());
                }

                int max = -1, min = 1000000;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > max) max = a[i];
                    if (a[i] < min) min = a[i];
                }

                Console.WriteLine("最大值:{0}", max);
                Console.WriteLine("最小值:{0}", min);
            }
            catch (Exception e)
            {
                Console.WriteLine("input error!");
            }
            /*******end********/

        }
    }
}
View Code

第4关:十进制数转换为二进制数

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch704
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int n = Convert.ToInt32(Console.ReadLine());
            if (n <= 0 || n > 2147483647)
            {
                Console.WriteLine("input error!");
                return;
            }
            Stack<int> s = new Stack<int>();
            while (n != 0)
            {
                s.Push(n % 2);
                n /= 2;
            }
 
            while (s.Count != 0)
            {
                Console.Write(s.Peek());
                s.Pop();
            }
            Console.WriteLine();
            /*******end********/

        }
    }
}
View Code

第5关:二维数组行列互换

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch705
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            
            int[,] a = new int[4, 4];
            int[,] b = new int[4, 4];
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    int.TryParse(Console.ReadLine(), out a[i, j]);
                }
            }

            Console.WriteLine("初始状况:");
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    Console.Write("{0} ", a[i, j]);
                }
                Console.WriteLine();
            }

            for (int j = 0; j < 4; ++j)
            {
                for (int i = 0; i < 4; ++i)
                {
                    b[j, i] = a[i, j];
                }
            }

            Console.WriteLine("互换后:");
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                   Console.Write("{0} ", b[i, j]);
                    
                }
                Console.WriteLine();
            }    
            /*******end********/

        }
    }
}
View Code

第6关:输出杨辉三角

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch706
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int n = Convert.ToInt32(Console.ReadLine());
            int[,] a = new int[n, n];
             
            for (int i = 0; i < a.GetLength(0); ++i)
            {
                for (int j = 0; j <= i; ++j)
                {
                    if(j == 0 || i == j)
                    {
                        a[i, j] = 1;
                    }
                    else
                    {
                        a[i,j] = a[i - 1,j] + a[i-1,j-1];
                    }
                    Console.Write(a[i,j].ToString() + " ");
                }
                Console.WriteLine();
            }
            /*******end********/

        }
    }
}
View Code

呼~~~终于搞完了,嘻嘻!

C#数组(EduCoder实训题目)

原文:https://www.cnblogs.com/QQ1723545340/p/13080644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!