C#循環結構程序設計(EduCoder實訓題目)


今天上午在EduCoder實訓平台進行了C#的實訓,主要是關於循環結構程序設計方面的,總共5道題,挺簡單的,也在這里作為我的第一個隨筆記錄一下,分享一下!

第1關:求1到100間整數的和

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

namespace ch501
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int Sum = 0;
            int x = 0;
            while (x < 101)
            {
            Sum = Sum + x;
            x = x + 1;
            }
            Console.WriteLine(Sum);
            Console.ReadKey();
            /*******end********/

        }
    }
}
View Code

第2關:求解出n以內所有能被5整除的正整數的乘積

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

namespace ch502
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int n = Convert.ToInt32(Console.ReadLine());
            int y = 1, i;
            for (i = 5; i <= n; i += 5) y *= i;
            Console.WriteLine($"s={y}");
            Console.ReadKey();
            /*******end********/

        }
    }
}
View Code

第3關:輸出任意一個數的逆序數

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

namespace ch503
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            string s = "";
            char[] c = Console.ReadLine().ToCharArray();
            for (int i = c.Length; i > 0; i--)
            {
                s += c[i - 1];
            }
            Console.WriteLine($"逆序數是:{s}");
            Console.ReadKey();
            /*******end********/

        }
    }
}
View Code

第4關:求所有水仙花數的和

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

namespace ch504
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int i, j, k;
            int sum = 0;
            for (int num = 100; num < 1000; num++)
            {
                i = num / 100;        //百位數字
                j = num % 100 / 10;  //十位數字
                k = num % 10;        //個位數字
                if (i * i * i + j * j * j + k * k * k == num)
                sum += num;
            }
            Console.WriteLine($"sum={sum}");
            Console.ReadKey();
            /*******end********/

        }
    }
}
View Code

第5關:斐波那契數列

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

namespace ch505
{
    class Program
    {
        static void Main(string[] args)
        {
            /******begin*******/
            int a = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine($"sum={sum(a)}");
            Console.ReadKey();
            }

            private static int F1(int a)
            {
            if (a == 1 || a == 2)
                return 1;
            else
                return F1(a - 1) + F1(a - 2);
            }
  
            private static int sum(int num)
            {
            int sum_new = 0;
            for (int i = 1; i <= num; i++)
            {
                sum_new = sum_new + F1(i);
            }
            return sum_new;
            /*******end********/

        }
    }
}
View Code

使用EduCoder平台進行實訓操作真的很方便,但是它也有很多不足的地方,它的答案都是固定的,輸出結果一個標點符號不一致都算不通過,不過總歸還是很不錯的。

這次C#實訓總共有18次,以后的題目有時間我也會在當天進行分享記錄的。

哈哈哈,如果真的有人看並且有用的話,希望能留下您的評論!


免責聲明!

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



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