1. 程式人生 > >7.28號C#作業

7.28號C#作業

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();
        }
     
        }
    }