0
点赞
收藏
分享

微信扫一扫

7.28号C#作业

兵部尚输 2022-04-06 阅读 33


1 、三角形等边 返回 1 等腰 2 其他返回3 不能构成三角形 4

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace sanjiaoxing

{

    // 三角形 等边 返回 1 等腰 2 其他返回3 不能构成三角形 4

    class Program

    {

        static void Main(string[] args)

        {

            int sum;

            Console.WriteLine("请分别输入三角形的三条边");

            int a = int.Parse(Console.ReadLine());

            int b = int.Parse(Console.ReadLine());

            int c = int.Parse(Console.ReadLine());

            if (!(a + b > c || a + c > b || b + c > a))

            {

                Console.WriteLine("输入的三条边无法构成三角形");

                sum = 4;

            }

            else

            {

                if (a == b&&b == c)

                {

                    sum = 1;

                    Console.WriteLine("所输入的三条边构成的是等边三角形");

                }

                else if ((a == b && a != c) || (a == c && a != b) || (b == c && b != a))

                {

                    sum = 2;

                    Console.WriteLine("所输入的三条边构成的是等腰三角形");

                }

                else

                {

                    sum = 3;

                    Console.WriteLine("所输入的三条边构成的是普通三角形");

                }     

            }

            Console.WriteLine("返回的值是{0}", sum);

            Console.ReadLine();

        }

    }

}


2 、狗年龄

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DogAge

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("请输入狗的年龄!");

            int age = int.Parse(Console.ReadLine());

            int[] arr = new int[20];

            arr[0] = 17;

            arr[1] = 23;

            arr[2] = 28;

            for (int i = 3; i < arr.Length; i++)

            {

                arr[i] = arr[i-1] + 4;

            }

            Console.WriteLine("狗相对于人的年龄是:{0}", arr[age-1]);

        }

    }

}


3. 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace diguiDemo

{

    //一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine(Digui(30));

            Console.ReadLine();

        }

        static int Digui(int t)

        {

            if (t <= 0)

            {

                return 0;

            }

            else if (t <= 2)

            {

                return 1;

            }

            else

            {

                return Digui(t - 2) + Digui(t - 1);

            }

        }

    }

}


4.请编程实现一个冒泡排序算法?using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace maopao

    //数组排序

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr1 = new int[] { 12, 4, 22, 5, 9, 36, 7, 14, 2, 18 };

            Console.WriteLine("数组排序前");

            foreach (int n in arr1)

                Console.Write(n + " ");

            Console.WriteLine();

            int j, temp;

            for (int i = 0; i < arr1.Length - 1; i++)

            {

                j = i + 1;

            aa:

                if (arr1[i] > arr1[j])

                {

                    temp = arr1[i];

                    arr1[i] = arr1[j];

                    arr1[j] = temp;

                    goto aa;

                }

                else

                    if (j < arr1.Length - 1)

                    {

                        j++;

                        goto aa;

                    }

            }

            Console.WriteLine("数组冒泡排序后");

            foreach (int n in arr1)

                Console.Write(n + " ");

            Console.WriteLine();

        }


        }

    }



举报

相关推荐

作业记录c#

3.29号作业

9.18号作业

9.14号作业

6月15号作业

9.20号作业实现钟表

0 条评论