c#实验2

c1.编程实现判断一个5位数是不是回文数。5位回文数是指个位数字与万位数字相同,十位数字与千位数字相同,如12321是回文数。

  static void Main(string[] args)
        {
            String sc = Console.ReadLine();
            if (sc.Substring(0, 1).Equals(sc.Substring(4, 1)) && sc.Substring(1, 1).Equals(sc.Substring(3, 1)))
            {
                Console.WriteLine("回文数");
            }
            else {
                Console.WriteLine("不是回文数");
            }
            Console.ReadLine();
        }

2.输出100以内能被3整除且个位数为6的所有整数

 static void Main(string[] args)
        {
            for (int i = 0; i <= 100; i++)
            {
                if (i % 10 == 6 && i % 3 == 0)
                {
                    Console.WriteLine(i);
                }
            }
            Console.ReadLine();
        }

3.我国古代有一道经典的数学题:“一只公鸡值5文钱,一只母鸡值3文钱,3只小鸡值1文钱,有钱100文,买鸡100只,问所买公鸡几只?母鸡几只?小鸡几只?”编写程序解决“百文买百鸡”问题,求出所有解。

 static void Main(string[] args)
        {
            int a, b;
            float c;
            for (a = 0; a <= 20; a++)
            {
                for (b = 0; b <= 34; b++)
                {
                    for (c = 0; c <= 300; c++)
                    {
                        if (a * 5 + b * 3 + c * 1 / 3 == 100)
                        {
                            Console.WriteLine("卖公鸡{0}只,母鸡{1}只,小鸡{2}只", a, b, c);
                        }
                    }
                }

            }
            Console.ReadLine();
        }
    }

4.求自然对数e的近似值,要求其误差小于0.00001,近似公式为:

image-20220320181227494

static void Main(string[] args)
        {
            double e = 1.0;
            for (int n = 1; ; n++)
            {
                double num = 1.0;
                for (int i = 1; i <= n; i++)
                {
                    num *= i;
                }
                num = 1.0 / num;
                if (num < 0.00001)
                {
                    Console.WriteLine("e的近似值" + e);
                    break;
                }
                else
                    e += num;
            }
            Console.ReadLine();
        }

5.求100—200之间的全部素数,每输出10个素数换行。

static void Main(string[] args)
        {
            int m=0;
            for (int i = 100; i < 201; i++)
            {
                bool a = true;
                for (int j = 2; j < i; j++) {
                    if (i%j == 0) {
                        a =false;
                    }
                }
                if (a) {
                    if (m % 10 == 0)
                        Console.WriteLine();
                    m++;
                    Console.Write(" " + i);
                }
            }
            Console.ReadLine();
        }

6、一个数如果恰好等于它的因子之和,就称其为“完数”。例如,6=1+2+3,1、2、3分别为6的因子,因此6是完数。编写程序找出1000以内的所有完数,并按下面的格式输出其因子:6 its factors are 1,2,3

     static void Main(string[] args)
        {
            for (int i = 1; i <= 1000; i++)
            {
                int[] num = new int[100];
                int m = 0;
                for (int j = 1; j <= (i / 2); j++)
                {
                    if (i % j == 0)
                    {
                        num[m] = j;
                        m++;
                    }
                }
                int n = 0;
                for (int z = 0; z < m; z++)
                {
                    n += num[z];
                }
                if (i == n)
                {
                    Console.Write(i + " its factors are");
                    for (int x = 0; x < m; x++)
                    {
                        Console.Write("," + num[x]);
                    }
                    Console.WriteLine();
                }
            }
            Console.ReadKey();
        }

7.输出* 形成的菱形,输入菱形的行数:如果为偶数行无法形成菱形,输出提示“请输入奇数行”;如果为奇数行,那么输出如下图所示的菱形:image-20220327161046551

        static void Main(string[] args)
        {
            bool bo = true;
            int n;
            while (bo)
            {
                Console.WriteLine("请输入菱形的行数:");
                n = int.Parse(Console.ReadLine());
                if (n % 2 == 0)
                {
                    Console.WriteLine("请输入奇数行!");
                }
                else
                {
                    for (int i = 0; i <= n / 2; i++)
                    {
                        for (int j = n / 2; j > i; j--)
                        {
                            Console.Write(" ");
                        }
                        for (int m = 0; m <= 2 * i; m++)
                            Console.Write("*");
                        Console.WriteLine();
                    }
                    for (int i = 0; i < n / 2; i++)
                    {
                        for (int j = 0; j <= i; j++)
                        {
                            Console.Write(" ");
                        }
                        for (int m = n - 2; m > 2 * i; m--)
                        {
                            Console.Write("*");
                        }
                        Console.WriteLine();
                    }
                }
            }
            Console.ReadKey();
        }
posted @ 2022-03-27 16:15  木_心  阅读(141)  评论(0)    收藏  举报