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

这些C#编程习题涵盖了基本的算术运算、时间格式转换、数字加密算法以及字符串操作。题目包括计算两数之和、差、积、商,将时间秒数转换为天小时分钟秒的格式,实现数字加密,输出字符串特定格式,以及大小写字母转换。此外,还有涉及数组处理、三角形判定及面积计算、日期处理和心率计算的相关练习。
摘要由CSDN通过智能技术生成

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

一、week 2

1.从键盘分别输入两个不为0的整数(前2行输入),分别输出这两个整数的和 差 积 商。

样例:

2

3

5 -1 6 0

using System;
namespace week2_hom1
{
    class Program
    {
        static void Main(string[] args)
        {
            string a1 = Console.ReadLine();
            string b1 = Console.ReadLine();
            int a = Convert.ToInt32(a1);
            int b = Convert.ToInt32(b1);
            int f_1 = a + b;
            int f_2 = a - b;
            int f_3 = a * b;
            int f_4 = a / b;
            Console.WriteLine("{0} {1} {2} {3}", f_1, f_2, f_3, f_4);
        }
    }
}

2.输入表示时间的整数,单位是秒。输出小时秒。

如果时间单位前的数值是0,则不输出该时间单位的数值。

样例:
3610
1小时10秒

using System;

namespace week1_hom2
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = Console.ReadLine();
            int t_s = Convert.ToInt32(a);
            int seconds= t_s % 60;
            int minutes = (t_s / 60) % 60;
            int hours = ((t_s / 60) / 60) % 24;
            int days = (t_s / 60) / 60 / 24;
            if(days!=0)
            {
                Console.Write("{0}天", days);
            }
            if (hours != 0)
            {
                Console.Write("{0}小时", hours);
            }
            if(minutes!=0)
            {
                Console.Write("{0}分", minutes);
            }
            if(seconds!=0)
            {
                Console.Write("{0}秒",seconds);
            }
            
            

        }
    }
}

3.对于一个4位整数(从0000-9999)按如下方式加密:将每位数字加7后对10取余,用余数替换原来的数字;然后将1,3位数字互换;2,4位数字互换。

输入一个4位数,输出加密后的结果。

样例:

5493

6021

如果时间单位前的数值是0,则不输出该时间单位的数值。

样例:
3610
1小时10秒

using System;
namespace week1_hom3
{
    class Program
    {
        static void Main(string[] args)
        {
            string a=Console.ReadLine();
            int b = Convert.ToInt32(a);
            int n_1,n_2,n_3,n_4;
            n_4 = (b%10+7)%10;
            n_3 = ((b%100)/10+7)%10;
            n_2 = ((b%1000)/100+7)%10;
            n_1 = (b/1000+7)%10;
            int final = n_3 * 1000 + n_4 * 100 + n_1 * 10 + n_2;
            Console.Write(final);      
        }
    }
}

4。输入一个字符串,按照样例格式输出。

样例:

Alice

Hi Alice,

Welcome to C# 2020!

using System;

namespace week1_hom4
{
    class Program
    {
        static void Main(string[] args)
        {
            string name=Console.ReadLine();
            Console.Write("Hi {0},\r\nWelcome to C# 2020!\r\n\r\nBest wishes!",name);
        }
    }
}

5.输入一个字母,如果它是小写字母则输出它的大写字母,如果它是大写字母输出它的小写字母。

样例1:

A

a

样例2:

b

B

using System;

namespace week1_hom5
{
    class Program
    {
        static void Main(string[] args)
        {
            string x =Console.ReadLine();
            char[] a = x.ToCharArray();
            int a_n = (int)a[0];
            if(a_n<91)
            {
                a_n = a_n + 32;
                Console.WriteLine((char)a_n);
            }
            else
            {
                a_n = a_n - 32;
                Console.WriteLine((char)a_n);
            }
        }
    }
}

二、week 3

1.在一行内输入5个整数,空格分隔。输出5个整数,逗号分隔。英文符号。

using System;

namespace week2_hom2
{
    class Program
    {
        static void Main(string[] args)
        {
            string a=Console.ReadLine();
            string[] b = a.Split(' ');
            for(int i=0;i<4; i++)
            {
                int k = Convert.ToInt32(b[i]);
                Console.Write(k);
                Console.Write(",");
            }
            int o = Convert.ToInt32(b[4]);
            Console.Write(o);
        }
    }
}


2定义一个三角型类,具有3个public的字段(double)为三角形的3边的长度。提供2个public的方法:

bool IsTriAngle()判断这3边能否构成一个三角形;
double Area() 求三角形的面积(如果不能构成三角形,返回值为-1)

输入三角形的3条边,用空格分隔。如果能构成三角形,直接输出三角形面积;否则输出-1

using System;

namespace week2_hom1
{
    class Program
    {
        static void Main(string[] args)
        {
            string intt = Console.ReadLine();
            string[] intt_1 = intt.Split(' ');
            double a1 = Convert.ToDouble(intt_1[0]);
            double a2 = Convert.ToDouble(intt_1[1]);
            double a3 = Convert.ToDouble(intt_1[2]);
            tri s = new tri();
            bool de=s.IsTriAngle(a1, a2, a3);
            if(de==true)
            {
                double ss = s.Area(a1, a2, a3);
                Console.WriteLine(ss);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
    class tri
    {
        public bool IsTriAngle(double a,double b,double c)
        {
            if(a + b > c && a + c > b && b + c > a)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public double Area(double a,double b,double c)
        {
            double k = (a + b + c) / 2;
            double s1 = Math.Pow(k * (k - a) * (k - b) * (k - c), 0.5);
            return s1;
        }
    }
}

3.创建一个Date类,要求能输入以下格式的日期:

第一种:MM/YYYY 构造函数接收2个整数
第二种:June,1992 构造函数接收一个字符串和一个整数

当用户输入其出生年月日时,能够计算出用户的年龄(到年即可。向下取整)。当用户输入的日期无意义或未来时间,输出invalid。(当前时间取系统时间,请查C#自带的取时间的函数)

using System;

namespace week2_hom3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = Convert.ToInt32(DateTime.Now.Year.ToString());
            int b = Convert.ToInt32(DateTime.Now.Month.ToString());
            string inp = Console.ReadLine();
            Date us = new Date();
            int len_1 = inp.Split(new char[] { '/' }).Length;
            int len_2 = inp.Split(new char[] { ',' }).Length;
            if (len_1 == 2)
            {
                us.Firsttype(inp, a, b);
            }
            else
            {
                if (len_2 == 2)
                {
                    us.Secondtype(inp, a, b);
                }
                else
                {
                    Console.WriteLine("invalid");
                }
            }
        }
    }
    class Date
    {
        public void Firsttype(string a, int year, int month)
        {
            string[] asp = a.Split(new char[] { '/'});
            int yearb = Convert.ToInt32(asp[1]);
            int monthb = Convert.ToInt32(asp[0]);
            int syear = year - yearb;
            int smonth = month - monthb;
            if (monthb < 0 || monthb > 12)
            {
                Console.WriteLine("invalid");
            }
            else
            {
                if (syear < 0)
                {
                    Console.WriteLine("invalid");
                }
                else
                {
                    if (smonth < 0)
                    {
                        int age = syear - 1;
                        string age_1 = Convert.ToString(age);
                        Console.WriteLine("{0}岁", age_1);
                    }
                    else
                    {
                        int age = syear;
                        string age_1 = Convert.ToString(age);
                        Console.WriteLine("{0}岁", age_1);
                    }
                }
            }

        }
        public void Secondtype(string a, int year, int month)
        {
            string[] month_t = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
            string[] asp = a.Split(new char[]{ ',' });
            string month_1 = asp[0];
            int year_1 = Convert.ToInt32(asp[1]);
            int syear = year - year_1;
            int i;
            for (i = 0; i < 12; i++)
            {
                if (month_1 == month_t[i])
                {
                    break;
                }
                else
                {
                    continue;
                }
            }
            if (i < 11)
            {
                int smonth = month - (i + 1);
                if (syear < 0)
                {
                    Console.WriteLine("invalid");
                }
                else
                {
                    if (smonth < 0)
                    {
                        int age = syear - 1;
                        Console.WriteLine("{0}岁", age);
                    }
                    else
                    {
                        Console.WriteLine("{0}岁", syear);
                    }
                }
            }
            else
            {
                Console.WriteLine("invalid");

            }
        }
    }
}

4.运动时,可以利用心率监测仪来查看心率是否处于安全范围内。其中最高心率=220-年龄;目标心率是最高心率的50%-85%(向下取整);

创建一个名称为HeartRates的类。这个类的的属性应该包含人的姓名、出生年份和当前年份。

类中还包含一个计算并返回年龄(以整年计算)的属性;一个计算并返回最高心率分方法;以及2个分别计算最低和最高目标心率的方法;

编写程序,实例化HeartRates类,输入个人姓名,出生年月日(空格分隔)。并输出对象的信息,包括姓名,出生年份;年龄;最高心率,最低目标心率,最高目标心率(空格分隔)

using System;
namespace week2_hom4
{
    class Program
    {
        static void Main(string[] args)
        {
            string a=Console.ReadLine();
            string[] a_1 = a.Split();
            string[] a_2 = a_1[1].Split(new char[] { '/'});
            int year = Convert.ToInt32(a_2[0]);
            HeartRates h = new HeartRates();
            int age = h.age(year);
            int m = h.Rate_m(age);
            int[] m_1 = h.Rate_p(m);
            Console.WriteLine("{0} {1}年 {2}岁 最高心率{3} 最低目标心率{4} 最高目标心率{5}", a_1[0], year, age, m, m_1[0], m_1[1]);

        }
    }
    class HeartRates
    {
        public int age(int year)
        {
            int a = Convert.ToInt32(DateTime.Now.Year.ToString());
            int b = a - year;
            return b;
        }
        public int Rate_m(int age)
        {
            int k = 220 - age;
            return k;
        }
        public int[] Rate_p(int age)
        {
            int lea_r =Convert.ToInt32(age * 0.5);
            int hig_r = Convert.ToInt32(age * 0.85);
            int[] f = new int[] { lea_r, hig_r };
            return f;
        }
    }
}

有任何问题欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值