# C#程序设计习题_2(西安交通大学)

C#程序设计习题_2(西安交通大学)

一、week 4

1.所谓孪生素数指的是间隔为2 的相邻素数,就像孪生兄弟。最小的孪生素数是(3, 5),在100 以内的孪生素数还有(3,5), (5,7), (11,13),(17,19),(29,31),(41,43),(59,61),(71,73) 总计有 8 组。(备注:每组孪生素数之间用英文逗号,分隔)

输入正整数,输出小于等于number的孪生素数的组数。

样例输入:
100

using System;

namespace week4_hom1
{
    class Program
    {
        static void Main(string[] args)
        {
            int peak = Convert.ToInt32(Console.ReadLine());
            
            int[] k = new int[peak];
            int l = 0;
            for(int i=2;i<peak;i++)
            {
                if(prnum(i) && prnum(i+2))
                {
                    k[l] = i;
                    l = l + 1;
                }
            }
            for(int i=0;i<l;i++)
            {
                if(i==l-1)
                {
                    Console.Write("({0},{1})", k[i], k[i] + 2);
                }
                else
                {
                    Console.Write("({0},{1}),", k[i], k[i] + 2);
                }

            }
        }
        static bool prnum(int a)
        {
            int b = a / 2;
            bool k = true;
            for (int i = 2; i < b + 1; i++)
            {
                if (a % i == 0)
                {
                    k = false;
                    break;
                }
            }
            return k;
        }
    }
}



2.求a+aa+aaa+aaaa+…+aa…a(第n项,n个a),其中a是1~9的整数。例如,

a=1,n=3时,式子为1+11+111;

当a=6,n=5时,式子为5+55+555+5555+55555。

格式: 第一行为输入a n 第二行为输出结果

using System;

namespace week4_hom2
{
    class Program
    {
        static void Main(string[] args)
        {
            string inp = Console.ReadLine();
            string[] inp_1 = inp.Split(' ');
            int a = Convert.ToInt32(inp_1[0]);
            int n = Convert.ToInt32(inp_1[1]);
            
            Console.WriteLine(cal(a, n));
        }
        static int pro(int a1, int n1)
        {
            int fin = 0;
            for (int i = 0; i < n1; i++)
            {
                fin += Convert.ToInt32(Math.Pow(10, i)) * a1;
            }
            return fin;
        }
        static int cal(int a2, int n2)
        {
            int fin = 0;
            for (int i = 1; i < n2 + 1; i++)
            {
                fin += pro(a2, i);
            }
            return fin;
        }
    }
}

3.输入公元年份和月份,输出该月份的天数。

(用switch……case语句)

using System;

namespace week4_hom3
{
    class Program
    {
        static void Main(string[] args)
        {
            string inp = Console.ReadLine();
            string[] inp_1 = inp.Split(new char[] { ',' });
            int year = Convert.ToInt32(inp_1[0]);
            int month = Convert.ToInt32(inp_1[1]);
            
            switch (year % 4)
            {
                case 0:
                    if (year % 100 == 0)
                    {
                        if (year % 400 == 0)
                        {
                            if (month == 2)
                            {
                                Console.WriteLine(29);
                            }
                            else
                            {
                                Console.WriteLine(mon(month));
                            }
                        }
                        else
                        {
                            Console.WriteLine(mon(month));
                        }
                    }
                    else
                    {
                        if (month == 2)
                        {
                            Console.WriteLine(29);
                        }
                        else
                        {
                            Console.WriteLine(mon(month));
                        }
                    }
                    break;
                case 1:
                    Console.WriteLine(mon(month));break;
                case 2:
                    Console.WriteLine(mon(month)); break;
                case 3:
                    Console.WriteLine(mon(month)); break;
                
            }
        }
        static int mon(int month)
        {
            int fin = 0;
            switch (month)
            {
                case 1:
                    fin = 31; break;
                case 2:
                    fin = 28; break;
                case 3:
                    fin = 31; break;
                case 4:
                    fin = 30; break;
                case 5:
                    fin = 31; break;
                case 6:
                    fin = 30; break;
                case 7:
                    fin = 31; break;
                case 8:
                    fin = 31; break;
                case 9:
                    fin = 30; break;
                case 10:
                    fin = 31; break;
                case 11:
                    fin = 30; break;
                case 12:
                    fin = 31; break;
            }
            return fin;
        }
    }
}

4.程序通过用户输入一个字符串(长度不超过30),由A、B、C、D、E五个字母组成,例如:ACBEEBBAD。

用户再输入一个字符,只能是A、B、C、D、E其中之一,然后再输入一个要插入的位置。

程序会将这个字符插入到字符串的指定位置前(第一个字符位置为0,第二个字符位置为1,依此类推),然后消除连续出现的三个相同的字符,直到没有连续三个相同的字符为止。

using System;

namespace week4_hom4
{
    class Program
    {
        static void Main(string[] args)
        {
            string inp = Console.ReadLine();
            string inp_1 = Console.ReadLine();
            int len = inp.Length;
            string inp_2 = inp_1.Split(new char[] { ' ' })[0];
            int inp_3 = Convert.ToInt32(inp_1.Split(new char[] { ' ' })[1]);
            string str = "";
            for(int i1 = 0; i1 < len; i1++)
            {
                if (i1 == inp_3)
                {
                    str += inp_2;
                }
                str += Convert.ToString(inp[i1]);
            }
            
            string str_final = "";
            while (str_final != str)
            {
                str_final = delll(delll(str));
                str = delll(str);
            }
            Console.WriteLine(str_final);
        }
         static string delll(string str)
        {
            int i = 0;
            while (i < str.Length - 3)
            {
                if (Convert.ToString(str[i]) == Convert.ToString(str[i + 1]) && Convert.ToString(str[i + 1]) == Convert.ToString(str[i + 2]))
                {
                    string o = Convert.ToString(str[i]);
                    str = str.Replace(o + o + o, "");
                    break;
                }
                i = i + 1;
            }
            return str;
        }
    }
}

二、week 5

1.定义Rectangle类,类中的两个属性Length和Width(其对应的私有字段为length和width,默认值为均为1)。

Length属性的set方法中验证其值必须在0~20(开区间)之间的浮点数。如果不满足保持其默认值。

方法:

public double Perimeter()计算长方形的周长

public double Area()计算长方形的面积

输入长方形的长和宽的值,输出长方形的周长、面积。

using System;

namespace week5_hom1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inp = Console.ReadLine();
            string[] inp_1 = inp.Split(' ');
            double l =Convert.ToDouble(inp_1[0]);
            double w =Convert.ToDouble(inp_1[1]);
            Rectangle a = new Rectangle();
            a.Length=l;
            a.Width=w;
            Console.WriteLine(a.Perimeter(a.Length,a.Width));
            Console.WriteLine(a.Area(a.Length, a.Width));
        }
    }
    public class Rectangle
    {

        private double length=1, width=1;
        public double Length
        {
            get
            {
                return length;
            }
            set
            {
                if(value>0 && value < 20)
                {
                    length = value;
                }

            }
        }
        public double Width
        {
            get
            {
                return width;
            }
            set
            {
                if (value > 0)
                {
                    width = value;
                }

            }
        }
        public double Perimeter(double a,double b)
        {
            double o = 2 * (a + b);
            return o;
        }
        public double Area(double a, double b)
        {
            return a*b;
        }
    }
}



2.读入若干(1-15个)整数(一行输入,空格分隔),每个数在10-100之间的整数包括10和100。

在读入每个数时,确认这个数的有效性(在10到100之间),并且若它和之前读入的数不一样,就把它存储到数组中,无效的数不存储。

读完所有数之后,仅显示用户输入的不同的数值。

using System;

namespace week5_hom2
{
    class Program
    {
        static void Main(string[] args)
        {
            string inp = Console.ReadLine();
            string[] inp_sm = inp.Split(' ');
            int[] fin = new int[15] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
            for(int i = 0; i < inp_sm.Length; i++)
            {
                int o = iny(Convert.ToInt32(inp_sm[i]));
                if(Array.IndexOf(fin, o) == -1)
                {
                    fin[i] = o;

                }
                else
                {
                    fin[i] = 0;
                }
            }
            int u = 0;
            for (int i = 0; i < fin.Length; i++)
            {

                if (fin[i] != 0)
                {
                    u++;
                    if (u==1)
                    {
                        Console.Write("{0}", fin[i]);
                    }
                    else
                    {
                        Console.Write(" {0}", fin[i]);
                    }
                }
            }


        }

        static int iny(int a)
        {
            if(a>=10 && a <= 100)
            {
                return a;
            }
            else
            {
                return 0;
            }
        }
    }
}

3.创建一个学生类。

字段:

学号(string类型)

成绩(用一个一维数组,存储一个学生的5门课成绩)

方法:将学生的5门课成绩由小到大顺序输出。

主函数中,声明3个学生对象,从控制台给每个学生的学号和5门课成绩赋值,调用方法输出。

using System;

namespace week5_hom3
{
    class Program
    {
        static void Main(string[] args)
        {
            string inp1 = Console.ReadLine();
            string inp2 = Console.ReadLine();
            string inp3 = Console.ReadLine();
            string[] inp1_1 = inp1.Split(' ');
            string[] inp2_1 = inp2.Split(' ');
            string[] inp3_1 = inp3.Split(' ');
            int[] inp1_2 = new int[5];
            int[] inp2_2 = new int[5];
            int[] inp3_2 = new int[5];
            inp1_2 = dou(inp1_1);
            inp2_2 = dou(inp2_1);
            inp3_2 = dou(inp3_1);
            Student a1 = new Student(inp1_1[0], inp1_2);
            Student a2 = new Student(inp2_1[0], inp2_2);
            Student a3 = new Student(inp3_1[0], inp3_2);
            Console.Write("{0} ", inp1_1[0]);
            a1.prin(inp1_2);
            Console.Write("{0} ", inp2_1[0]);
            a1.prin(inp2_2);
            Console.Write("{0} ", inp3_1[0]);
            a1.prin(inp3_2);

        }
        static int[] dou(string[] a)
        {
            int[] fin = new int[5];
            for(int i = 1; i < a.Length; i++)
            {
                fin[i - 1] = Convert.ToInt32(a[i]);
            }
            return fin;
        }
    }
    public class Student
    {
        public String stun;
        public int[] gra = new int[5];
        public Student(string str,int[] inp)
        {
            stun = str;
            gra = inp;
        }
        public void prin(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                int temp = a[i];
                int j = i;
                while ((j > 0) && (a[j - 1] > temp))
                {
                    a[j] = a[j - 1];
                    --j;
                }
                a[j] = temp;
            }
            for(int i= 0; i < a.Length; i++)
            {
                if (i == 0)
                {
                    Console.Write("{0}", a[i]);
                }
                else
                {
                    Console.Write(" {0}", a[i]);
                }
               
            }
            Console.WriteLine();
        }

    }
}

有任何问题欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值