一 单重循环

第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 a = 0;
            int i = 0;
            while(i <= 100){
                a += i;
                i++;
            }
            Console.WriteLine(a);
			/*******end********/
        }
    }
}

第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 a = int.Parse(Console.ReadLine());
            int b = 1;
            int i = 0;
            while(i < a){
                if(a % 5 == 0){
                    b = b * a; 
                }
                a--;
            }
            Console.WriteLine($"s={b}");
			/*******end********/
        }
    }
}

第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*******/
            int a = int.Parse(Console.ReadLine());
            int b = 0;
            int c = a;
            while (c != 0){
                int d = c % 10;
                b = b * 10 + d;
                c = c / 10;
            }
            Console.WriteLine($"逆序数是:{b}");
			/*******end********/

        }
    }
}

第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 = 999;
            int a = 0;
            while(i > 100){
                int b = (int)Math.Pow((i / 100) , 3);
                int c = (int)Math.Pow((i / 10) % 10 , 3);
                int d = (int)Math.Pow((i % 10) , 3);
                if(i == b + c + d){
                    a = a + i ;
                }
                i--;
            }
            Console.WriteLine($"sum={a}");
			/*******end********/

        }
    }
}


第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 = int.Parse(Console.ReadLine());
            long i = 1;
            long j = 1;
            long b = 0;
            if(a >= 1){
                b += i;
            }
            if(a >= 2){
                b += j;
            }
            for(int k = 3; k <= a; k++){
                long z = i + j;
                b += z;
                i = j;
                j = z;
            }
            Console.WriteLine($"sum={b}");
			/*******end********/
        }
    }
}

二 多重循环

第1关:九九乘法口诀

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ch601
{
    class Program
    {
        static void Main(string[] args)
        {
			/******begin*******/
	    int a = int.Parse(Console.ReadLine());
            if(a < 1 || a > 9){
                Console.WriteLine("input error!");
                return;
            }
			for(int i = 1; i <= a; i++){
                for(int j = 1; j <= i; j++){
                    Console.Write($"{i}*{j}={i * j}\t");
                }
                Console.WriteLine();
            }
			
			/*******end********/
        }
    }
}

第2关:多重循环-求阶乘之和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ch602
{
    class Program
    {
        static void Main(string[] args)
        {
			/******begin*******/
	    int n = int.Parse(Console.ReadLine());
            long s = 0;
            long b = 1;
            if(n < 0){
                s = 0;
            }
            else if(n == 0){
                s = 1;
            }
            else{
                for(int i = 1; i <= n; i++){
                    b = b * i;
                    s += b;
                }
            }
            Console.WriteLine(s);
			/*******end********/
        }
    }
}

第3关:多重循环-求n以内所有素数的和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ch603
{
    class Program
    {
        static void Main(string[] args)
        {
			/******begin*******/
	    int n = int.Parse(Console.ReadLine());
            int a = 0;
            for(int i = 2; i <=n; i++){
                bool isPrime = true;
                for(int j = 2; j * j <= i; j++){
                    if(i % j == 0){
                        isPrime = false;
                        break;
                    }
                }
                if(isPrime){
                    a += i;
                }
            }
            Console.WriteLine($"素数的和:{a}");
			/*******end********/
        }
    }
}

(注: 这串代码中,isPrime是用于判断一个整数是否为素数的函数,bool是用来逻辑判断 真ture 或 假false 的函数)

第4关:多重循环-寻找完数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ch604
{
    class Program
    {
        static void Main(string[] args)
        {
			/******begin*******/
	    int n = int.Parse(Console.ReadLine());
            for(int i = 2; i <= n; i ++){
                int a = 0;
                for(int j = 1; j < i; j ++){
                    if(i % j == 0){
                        a = a + j;
                    }
                }
                if(a == i){
                    Console.WriteLine(i);
                }
            }
			/*******end********/
        }
    }
}

此答案为自己所做,并非原题答案,仅供参考

此作者没有提供个人介绍。
最后更新于 2026-03-21