c#课后习题第二章

2.1输入三个整数,求这三个数的最大公约数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tr0201
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int n1, n2, n3, n;
            Console.WriteLine("请输入三个整数:");
            Console.Write("n1=");
            n1=Convert.ToInt16(Console.ReadLine());
            Console.Write("n2=");
            n2 = Convert.ToInt16(Console.ReadLine());
            Console.Write("n3=");
            n3 = Convert.ToInt16(Console.ReadLine());
            n = n1;
            while(n>1)
            {
                if ((n1 % n == 0) && (n2 % n == 0) && (n3 % n == 0)) break;
                n--;
            }
            Console.WriteLine("最大公约数为{0}",n);
            Console.ReadLine();

        }
    }
}

2.3利用枚举类型计算当前日期以及前一天和后一天的星期值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tr0203
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] month = new int[12];
            month =new int [12] {1,2,3,4,5,6,7,8,9,10,11,12 };
            int[] day = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
            string[] week = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };

            Console.Write("请输入该天月份(阿拉伯数字):");
            int  x = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入该天日期(阿拉伯数字):");
            int y= Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入改天星期(汉字):");
            string z = Convert.ToString(Console.ReadLine());

            
                if(z==week[0])
                {
                    Console.WriteLine("前一天为星期天");
                    Console.WriteLine("后一天为星期二");
                   
                }
                else if(z == week[6])
                {
                    Console.WriteLine("前一天为星期六");
                    Console.WriteLine("后一天为星期一");
               
                }
                else
                {for(int i=1;i<5;i++)
                {
                    Console.WriteLine("前一天为{0}", week[i - 1]);
                    Console.WriteLine("后一天为{0}", week[i + 1]);
                    break;
                }
                   
            
                }
         

            if ((x==1)||(x==3)||(x==5)||(x == 7) || (x == 8)|| (x == 10) || (x == 12))
            {
               if(x==1)
                {
                    if(y==1)
                    {
                        Console.WriteLine("前一天为{0},{1}", month[11], day[30]);
                        Console.WriteLine("后一天为{0},{1}", month[0], day[1]);
                    }
                    else if(y==31)
                    {
                        Console.WriteLine("前一天为{0},{1}", month[0], day[29]);
                        Console.WriteLine("后一天为{0},{1}", month[1], day[0]);
                    }
                    else
                    {
                        Console.WriteLine("前一天为{0},{1}", month[0], day[y-2]);
                        Console.WriteLine("后一天为{0},{1}", month[0], day[y]);
                    }
                }
               else if(x==12)
                {
                    if(y==1)
                    {
                        Console.WriteLine("前一天为{0},{1}", month[10], day[30]);
                        Console.WriteLine("后一天为{0},{1}", month[11], day[1]);
                    }
                    else if(y==31)
                    {
                        Console.WriteLine("前一天为{0},{1}", month[10], day[29]);
                        Console.WriteLine("后一天为{0},{1}", month[0], day[0]);
                    }
                    else
                    {
                        Console.WriteLine("前一天为{0},{1}", month[11], day[y-2]);
                        Console.WriteLine("后一天为{0},{1}", month[11], day[y]);
                    }
                }
               else
                {
                    if(y==1)
                    {
                        Console.WriteLine("前一天为{0},{1}", month[x-2], day[30]);
                        Console.WriteLine("后一天为{0},{1}", month[x-1], day[1]);
                    }
                    else if(y==31)
                    {
                        Console.WriteLine("前一天为{0},{1}", month[x - 1], day[29]);
                        Console.WriteLine("后一天为{0},{1}", month[x ], day[0]);
                    }
                    else
                    {
                        Console.WriteLine("前一天为{0},{1}", month[x - 1], day[y-2]);
                        Console.WriteLine("后一天为{0},{1}", month[x-1], day[y]);
                    }
                }
            }
            else if(x==2)
            {
                if(y==28)
                {
                    Console.WriteLine("前一天为{0},{1}", month[x - 1], day[y - 2]);
                    Console.WriteLine("后一天为{0},{1}", month[x ], day[0]);
                }
                else if(y==1)
                {
                    Console.WriteLine("前一天为{0},{1}", month[x - 2], day[30]);
                    Console.WriteLine("后一天为{0},{1}", month[x-1], day[1]);
                }
                else
                {
                    Console.WriteLine("前一天为{0},{1}", month[x - 1], day[y-2]);
                    Console.WriteLine("后一天为{0},{1}", month[x - 1], day[y]);
                }
            }
            else
            {
                if(y==30)
                {
                    Console.WriteLine("前一天为{0},{1}", month[x - 1], day[28]);
                    Console.WriteLine("后一天为{0},{1}", month[x ], day[0]);
                }
                else if(y==1)
                {
                    Console.WriteLine("前一天为{0},{1}", month[x - 2], day[29]);
                    Console.WriteLine("后一天为{0},{1}", month[x - 1], day[y]);
                }
                else
                {
                    Console.WriteLine("前一天为{0},{1}", month[x - 1], day[y-2]);
                    Console.WriteLine("后一天为{0},{1}", month[x - 1], day[y]);
                }
            }Console.ReadKey();

        }
    }
}

2.5输入n(1~9之间的整数),输出杨辉三角

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tr0205
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            int n;
            Console.WriteLine("请输入一个整数n:");
            n=Convert.ToInt32(Console.ReadLine());
            int m = n + 1;
            int[,] array = new int[m, m];
            
            for (i=1;i<=m;i++)
            {for (int k = 1; k < m - i; k++) //打印空格
            {
                Console.Write(" ");
            }
                for(j=1;j<=i;j++)
                {
                    if ((j==1)||(i==j))
                    {
                        array[i, j] = 1;
                    }
                    else
                    {
                        array[i, j] = array[i - 1, j - 1] + array[i - 1, j];
                    }
                    Console.Write(array[i, j].ToString() + " ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();

        }
    }
}

你可能感兴趣的:(c#,c#,开发语言,linq)