C#基础-045 练习题

来源:互联网 发布:python中执行shell脚本 编辑:程序博客网 时间:2024/06/05 18:36
 /*          1、求出1—100内的所有质数的和。(质数:只能被1和本身整除(),1不是质数也不是合数)         */        public static void Task01()        {            int sum = 0;            for (int i = 2; i < 101; i++)            {                if (IsPrime(i))                {                    //Console.WriteLine(i);                    sum += i;                }            }            Console.WriteLine("1—100内的所有质数的和为{0}", sum);            //Console.ReadKey();        }        static bool IsPrime(int r)        {            for (int i = 2; i <= r / 2; i++)            {                if (r % i == 0)                {                    return false;                }            }            return true;        }
 /*  2、打印图形,例如传入2           输出          AA          BBBB          传入4 输出          AA          BBBB          CCCCCC          DDDDDDDD         */        public static void Task02()        {            Console.WriteLine("请输入要打印的行数");            int lines = int.Parse(Console.ReadLine());            for (int i = 1; i <= lines; i++)            {                for (int j = 0; j < 2 * i; j++)                {                    Console.Write((char)('A' + i - 1));                }                Console.WriteLine();            }        }
  /*  3、写一个方法把字符串中的大写字母转换成小写,小写字母转化成大写。         */        public static void Task03()        {            string str = "sffsDdssgCHHJHVJHVJHDDSDFGdg";            Console.WriteLine("大小写翻转后为{0}", Conversion(str));        }        static string Conversion(string str)        {            char word;            string words = string.Empty;            for (int i = 0; i < str.Length; i++)            {                word = str[i];                if (word >= 'A' && word < 'Z')                {                    words += char.ToLower(word);                }                else if (word >= 'a' && word < 'z')                {                    words += char.ToUpper(word); ;                }            }            return words;        }
  /* 4、合并数组 把两个数组中索引相同的元素进行相加           例如:                arr1 = {1,2,3,4,5,6}                arr2 = {2,3,4,5,6,7,8}           合并后 {3,5,7,9,11,13,8}          【注意两个数组的长度不确定  第一个数组的长度可能大于第二个数组的长度】       */        public static void Task04()        {            Random ran = new Random();            Console.WriteLine("第一个随机数组:");            int[] numberArr1 = new int[ran.Next(5, 11)];            for (int i = 0; i < numberArr1.Length; i++)            {                int a = ran.Next(1, 10);                if (!(IsContains(a, numberArr1)))                {                    numberArr1[i] = a;                }            }            Show(numberArr1);            Console.WriteLine("第二个随机数组:");            int[] numberArr2 = new int[ran.Next(5, 11)];            for (int i = 0; i < numberArr2.Length; i++)            {                int b = ran.Next(1, 10);                if (!(IsContains(b, numberArr2)))                {                    numberArr2[i] = b;                }            }            Show(numberArr2);            int bigLength = 0;            int smallLength = 0;            if (numberArr1.Length > numberArr2.Length)            {                smallLength = numberArr2.Length;                bigLength = numberArr1.Length;            }            else            {                bigLength = numberArr2.Length;                smallLength = numberArr1.Length;            }            int[] resultArr = new int[bigLength];            for (int i = 0; i < smallLength; i++)            {                resultArr[i] = numberArr1[i] + numberArr2[i];            }            for (int i = smallLength; i < bigLength; i++)            {                if (numberArr1.Length > numberArr2.Length)                {                    resultArr[i] = numberArr1[i];                }                else                {                    resultArr[i] = numberArr2[i];                }            }            Console.WriteLine("合并后的结果为:");            Show(resultArr);        }        static bool IsContains(int number, int[] arr)        {            for (int i = 0; i < arr.Length; i++)            {                if (arr[i] == number)                {                    return true;                }            }            return false;        }        static void Show(int[] arr)        {            for (int i = 0; i < arr.Length; i++)            {                Console.Write("{0}  ", arr[i]);            }            Console.WriteLine();        }
  /*        5、根据规律写方法        1 1 2 3 5 8 13 21。。。        传入6        返回13        传入7        返回21        */        public static void Task05()        {            Console.WriteLine("请输入一个大于二的整数,回车键得到相应的斐波那契数");            int num = int.Parse(Console.ReadLine());            Console.WriteLine(FabniceNumber(num));        }        static int FabniceNumber(int num)        {            int[] number = new int[num + 1];            number[0] = 1;            number[1] = 1;            for (int i = 2; i < num + 1; i++)            {                number[i] = number[i - 1] + number[i - 2];            }            return number[num];        }
 /*        6、打印杨辉三角                     1                    1 1                   1 2 1                   1 3 3 1                 1 4 6 4 1               1 5 10 10 5 1        */        public static void Task06()        {            Console.WriteLine("请输入需要打印的杨辉三角的行数");            int num = int.Parse(Console.ReadLine());            int[,] arr = new int[num, num];            for (int i = 0; i < num; i++)            {                for (int j = 0; j < num - i; j++)                {                    Console.Write("    ");                }                for (int j = 0; j <= i; j++)                {                    //第一种方法                    //if (j == 0 || j == i)                    //{                    //    arr[i, j] = 1;                    //}                    //else                    //{                    //    arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1];                    //}                    //方法二:三目运算                    arr[i, j] = j == 0 || j == i ? 1 : arr[i - 1, j] + arr[i - 1, j - 1];                    Console.Write(arr[i, j].ToString() + "      ");                }                Console.WriteLine();            }            Console.ReadKey();        }
 /*        7、统计字符串中的数字字符,大写字母,小写字母的个数  【注 out】       */        public static void Task07()        {            string str = "afH856";            int numberCount = 0;            int bigWordsCount = 0;            int smallWordsCount = 0;            Count(str, out bigWordsCount, out smallWordsCount,out numberCount);            //方法1            //for (int i = 0; i < str.Length; i++)            //{            //    if (str[i] >= 'A' && str[i] < 'Z')            //    {            //        bigWordsCount++;            //    }            //    else if (str[i] >= 'a' && str[i] < 'z')            //    {            //        smallWordsCount++;            //    }            //    else if ( str[i] >= '0' && str[i] <= '9')            //    {            //        numberCount++;            //    }            //}            Console.WriteLine("{0}中有大写字母{1}个,小写字母{2}个,数字{3}个", str, bigWordsCount, smallWordsCount, numberCount);        }        //方法二        static void Count(string str, out int bigWordsCount, out int smallWordsCount ,out int numberCount)        {            numberCount = 0;            bigWordsCount = 0;            smallWordsCount = 0;            for (int i = 0; i < str.Length; i++)            {                if (str[i] >= 'A' && str[i] < 'Z')                {                    bigWordsCount++;                }                else if (str[i] >= 'a' && str[i] < 'z')                {                    smallWordsCount++;                }                else if (str[i] >= '0' && str[i] <= '9')                {                    numberCount++;                }            }        }
     /*        8、编写一个学生类:           学生类包括:           字段:名字、年龄、所属省份、所属市           测试:           1产生5个学生对象。字段值通过构造器自定义即可。           2根据省份输出学员中该省份的所有学生信息。           3根据所属市输出学员中该市的所有学生信息。       */        public static void Task08()        {            Student[] stu = new Student[5];            stu[0] = new Student("小明", 23, "河南省", "郑州");            stu[1] = new Student("小红", 25, "河北省", "石家庄");            stu[2] = new Student("小张", 33, "河南省", "开封");            stu[3] = new Student("小李", 43, "山东省", "济南");            stu[4] = new Student("小宋", 28, "山东省", "聊城");            Console.WriteLine("-----------------------------");            ProvinceShow(stu, "山东省");            ProvinceShow(stu, "河北省");            ProvinceShow(stu, "河南省");            Console.WriteLine("-----------------------------");            CityShow(stu, "开封");            CityShow(stu, "济南 ");            CityShow(stu, "石家庄");        }        static void ProvinceShow(Student[] arr, string province)        {            for (int i = 0; i < arr.Length; i++)            {                if (province == arr[i].Province)                {                    arr[i].Show();                }            }        }        static void CityShow(Student[] arr, string city)        {            for (int i = 0; i < arr.Length; i++)            {                if (city == arr[i].City)                {                    arr[i].Show();                }            }        }
  /*        9、编写一个立体几何类SolidGeometry,里面有一个求体积GetVolume的方法,有一个求两个立体几何距离的方法           编写一个Sphere类,字段包含:半径,x坐标,y坐标,z坐标           1.求一个球体的体积           2.判断两个球是否相离       */        public static void Task09()        {            Sphere s = new Sphere(3.2, 2.2, 3.3, 4.4);            s.GetVolume(s.Radius);            Sphere q = new Sphere(4.5, 1.8, 4.9, 5.3);            s.GetInfo(s, q);        }
 /*        10.创建一个玩家类,           字段:类型、名称、生命值、魔法值、攻击力、生存状态           初始化:生命值、魔法值、攻击力的初值分别为800、100、50;           玩家类有一个攻击方法:           public void Attack(Player player)。           测试要求:           1.产生两个玩家对象:野蛮人和魔法师(用类型区分)           2.野蛮人每次攻击造成的伤害在[攻击力-10] 到[攻击力+10]之间(这个伤害值是一个随机值),           3.野蛮人在攻击时有25%的几率产生1次暴击,每次暴击产生的伤害是原来的3倍;           4.魔法师每次攻击造成的伤害在攻击力的80%~100%之间(也是一个随机数),魔法师每次攻击消耗18点魔法,它会额外减少对方12%的生命值。           5.让两个对象进行PK,就是你打我一下,我打你一下,直到有一方死亡为止;野蛮人先攻击。       */        public static void Task10()        {            Player player = new Player("亚瑟", "野蛮人", 800, 100, 50, "正常");            Player enemy = new Player("虞姬", "法师", 800, 100, 50, "正常");            Random random = new Random();            int count = 1;            while (true)            {                Console.WriteLine("------------第{0}回合---------------", count);                player.PlayerAttack(enemy, random.Next(1, 5));                enemy.ToString1();                if (enemy._Hp <= 0)                {                    Console.WriteLine("虞姬牺牲了");                    break;                }                else                {                    Console.WriteLine("虞姬还有{0}血", enemy._Hp);                }                Console.WriteLine("***************");                enemy.EnemyAttack(player);                player.ToString2();                if (player._Hp <= 0)                {                    Console.WriteLine("亚瑟玩完了");                    break;                }                else                {                    Console.WriteLine("亚瑟还有{0}血", player._Hp);                }                count++;                Console.WriteLine();                Console.WriteLine();            }