C#數組(EduCoder實訓題目)


這才是今天的實訓,沒想到吧,哈哈哈!

第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

呼~~~終於搞完了,嘻嘻!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM