结构体共用变量 递归

结构体共用变量

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

namespace 结构体复习共用变量
{
    class Program
    {
        public int renfen;
        public int dianfen;
        public int caiquan()
        {
            Console.Write("你要出的是(剪刀石头布)");
            string ren = Console.ReadLine();
            string diannao = "剪石布";
            Random ran = new Random();
            int a = ran.Next(3);
            string aa = diannao.Substring(a, 1);
            if (ren == "剪刀")
            {
                switch (aa)
                {
                    case "":
                        //Console.Write("平局"); 
                        //break;
                        return 0;

                    case "":
                        //Console.Write("你输了");
                        //break;
                        return 1;
                    case "":
                        //Console.Write("你赢了");
                        //break;
                        return 2;
                    default:
                        return 3;
                }
            }
            else if (ren == "石头")
            {
                switch (aa)
                {
                    case "":
                        // Console.Write("你赢了");
                        // break;
                        return 2;
                    case "":
                        //Console.Write("平局");
                        //break;
                        return 0;
                    case "":
                        //Console.Write("你输了");
                        //break;
                        return 1;
                    default:
                        return 3;
                }
            }
            else if (ren == "")
            {
                switch (aa)
                {
                    case "":
                        //Console.Write("你输了");
                        //break;
                        return 1;
                    case "":
                        //Console.Write("你赢了");
                        //break;
                        return 2;
                    case "":
                        //Console.Write("你平局了");
                        //break;
                        return 0;
                    default:
                        return 3;
                }
            }
            else
            {
                //Console.Write("输入错误");
                return 3;
            }
        }




        static void Main(string[] args)
        {
            Program hanshu = new Program();
            //创建一个储存学生学号姓名成绩的结构体
            //根据人数分别输入然后放置在集合中
            //所有数据录入完成之后,进行打印



            //猜拳,5局3胜
            //要求使用公共变量
            while (hanshu.dianfen < 3 && hanshu.renfen < 3)
            {
                int shuchu = hanshu.caiquan();
                switch (shuchu)
                {
                    case 0:
                        hanshu.dianfen += 0;
                        hanshu.renfen += 0;
                        Console.WriteLine("平局");
                        break;
                    case 1:
                        hanshu.dianfen += 1;
                        hanshu.renfen += 0;
                        Console.WriteLine("您输了");
                        break;
                    case 2:
                        hanshu.dianfen += 0;
                        hanshu.renfen += 1;
                        Console.WriteLine("您赢了");
                        break;
                    case 3:
                        Console.WriteLine("此轮无效");
                        break;
                }
                Console.WriteLine("现在比分:人{0}分,电脑{1}分", hanshu.renfen, hanshu.dianfen);

            }
            if (hanshu.renfen == 3)
            {
                Console.WriteLine("人赢了");
            }
            else if (hanshu.dianfen == 3)
            {
                Console.WriteLine("电脑赢了");
            }
            Console.ReadLine();





        }
    }
}

递归

一、概念 conception

函数本身调用函数自身,直到符合某一条件不在继续调用

二、应满足条件 factor

(1)有反复执行的过程(调用自身);

(2)有跳出反复执行过程的条件(函数出口)

三例子

阶乘计算 n!=n*(n-1)*(n-2)*(n-3)*...*1(n>0)

namespace 递归整理

{

        class   void   Main  (string[]  arge)

        {

         Program  digui=new  Program();

         console.writeline(digui.recursion(5));

         console.readline();

         }

         public   int   recursion (int  i)

         {

               int   sum=0;

               if(i==0)           //循环执行函数recursion,直到满足条件i=1;  跳出循环

              {

                return 1; 

               }

                sum=i*recursion(i-1);

          }//从i=1开始,到i=5结束,计算sum值(回归到sum)

}

四、注意事项notice

1、递归中必须要存在一个循环结束的条件。

2、递归函数的每次调用都需要栈来储存,如果次数太多的话容易造成栈溢出。

 五、例题

羊问题

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

namespace 递归
{
    class Program
    {
        public int yang(int a)
        {
            int sum = 0;
            if (a == 7)
            {
                return 2;
            }
            sum = (yang(a + 1) + 1) * 2;
            return sum;
        }


        static void Main(string[] args)
        {
            //卖羊 每过一个村庄卖出1/2又1只,经过7个村庄剩两支
            //问开始赶了几只
            Program hanshu = new Program();

            int zhong = hanshu.yang(0);
            Console.Write(zhong);
            Console.ReadLine();


        }
    }
}
愿我有生之年,得见您君临天下。 吾辈必当勤勉,持书仗剑耀中华。
原文地址:https://www.cnblogs.com/bloodPhoenix/p/5637757.html