三月十一号

今天的主要内容使复习穷举和迭代,以及能够让计算机可以较为正常的和人类的对话,说白了也就是输入错误要你无限制重新输入,直到正确为止,较麻烦的。下面会有很多特殊案例的整理,也希望自己能从新稳固一下。。。。。

经典案例百文百鸡的类型,注意有没有特殊的要求

  //百鸡百钱:公鸡2文钱一只,
            //母鸡1文钱一只,小鸡半文钱一只,
            //总共只有100文钱,
            //如何在凑够100只鸡的情况下刚好花完100文钱?

            int i = 0;
            for (int g = 0; g <= 50; g++)
            {
                for (int m = 0; m <= 100; m++)
                {
                    for (int x = 0; x <= 200; x++)
                    {
                        if (2 * g + m * 1 + 0.5 * x == 100 && g + m + x == 100)
                        {
                            i++;
                            Console.WriteLine("这是第" + i + "中总共有" + g + "公鸡," + m + "母鸡," + x + "小鸡");
                        }
                    }
                }
            }
            Console.WriteLine(i);
            Console.ReadLine();

经验证共有34种,之前33是因为个g,x,m的赋值是从1开始的。

//兔子生兔子问题,一开始只有1对幼兔
            //问第几个月的时候有多少对兔子,分别多少对
            //小兔=上月幼兔

           //成兔=上月小兔+上月成兔
            //幼兔=本月成兔

这是讲义上的经典题目,注意看好赋值的问题,之前做错是因为幼兔和成兔的赋值出现问题,虽然两个数量是相等的,但是执行起来是不一样的

 Console.Write("请输入月份");
            int a = int.Parse(Console.ReadLine());
            int y = 1; int x = 0; int c = 0; int z = 1;
            for (int i = 1; i <= a; i++)
            {
                if (i == 1)
                {
                    y = 1;
                    x = 0;
                    c = 0;
                }
                else
                {
                    c = x + c;
                    x = y;
                    y = c;
                    z = x + y + c;
                }
            }

            Console.WriteLine("" + a + "月总共有小兔" + z + 分别是:" + y + "幼兔," + x + "小兔," + c + "成兔");
            Console.ReadLine()

下面是和计算机对话的问题这是一个比较麻烦的过程,重复输入知道正确为止

   //异常语句try catch finally
            try//保护执行里面的代码段,若其中一句有错误,直接跳转到catch
            {//不会管下面的内容
                Console.Write("请输入一个整数");
                int a = int.Parse(Console.ReadLine());
                Console.WriteLine("hello");
            }
            catch //try中发现异常,直接执行,若try中无错,不执行
            {
                Console.WriteLine("输入有误!");
            }
            finally//不管上面有没有错,都需要执行!
            {
                Console.WriteLine("谢谢使用,再见!");
            }
            Console.ReadLine();

关于爱不爱的问题

 for (int i = 1; i <= 3; i++)
            {
                if (i == 1)
                {
                    Console.Write("你到底爱不爱我");
                }
                if (i == 2)
                {
                    Console.Write("问你呢,你到底爱不爱我");
                }
                if (i == 3)
                {
                    Console.Write("你聋了吗,到底爱不爱我");
                }
                string a = Console.ReadLine();
                if (a == "")
                {
                    Console.WriteLine("我也爱你");
                    System.Threading.Thread.Sleep(2000);//2000毫秒
                    Console.WriteLine("从此王子和公主");
                    break;
                }
                else
                {
                    if (i == 3)
                    {
                        Console.WriteLine("我们拉倒吧");
                    }
                }
            }
            Console.ReadLine();

关于 类的问题有两种 string 和math

math

  //Math类
            double a = 2.5;
            Console.WriteLine(Math.Ceiling(a));//取上线
            Console.WriteLine(Math.Floor(a));//取下线
            Console.WriteLine(Math.PI * a);//π  圆周率
            Console.WriteLine(Math.Sqrt(a));//开平方根
            Console.WriteLine(Math.Round(a));//四舍五入
            //注意:奇数.5的情况下,取上线
            //偶数.5的情况下,取下线

string

  //String        
            string a = "    abCdefgd8    ";
            int c = a.IndexOf("d");//从前面开始找,找到第一个,数它的索引号
            int d = a.LastIndexOf("d");
            bool b = a.Contains("dd");//是否包含此字符串
            Console.WriteLine(d);

            int e = a.Length;//长度
            Console.WriteLine(b);
 string a = "    abCdefgd8    ";
            string c = a.Trim();//去掉前后空格
            Console.Write(c);
            Console.Write("
");
            string d = a.TrimStart();//去掉前空格
            Console.Write(d);
            Console.Write("
");
            string e = a.TrimEnd();//去掉后空格
            Console.Write(e);
            Console.Write("
");
            string f = a.ToUpper();//全部将小写字母转换为大写
            Console.WriteLine(f);
            string g = a.ToLower();//全部将大写字母转换为小写
            Console.WriteLine(g);
 string a = "    abCdefgd8    ";
            string h = a.Substring(4);
            //里面一个值,表示从这个索引开始一直截取到最后
            Console.WriteLine(h);
            Console.WriteLine(a.Substring(8));

            //a = a.Substring(8);//若果不重新赋值,a是没有变化的

            //两个值,表示从哪个索引号开始,截取多少长度
            string i = a.Substring(4, 3);
            Console.WriteLine(i);


            a = a.Replace("de", "DE");

            Console.WriteLine(a);
            string j = "2012 12 23";
            string[] aa = j.Split();//分割字符串,以什么字符
            foreach (string m in aa)
            {
                Console.WriteLine(m);
            }
原文地址:https://www.cnblogs.com/Duriyya/p/5267475.html