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