C编程题(36)

 

猴子选大王:
   ① N 只猴子站成一行,每隔 M 只从头到尾报数,反复进行,报过数的退出,打 印每次退出的猴子的编号,直到剩下一只为止。
   ② N 只猴子站成一行,每 M 只报数。先从头到尾,报到尾后,再返回从尾到头报数,打印每次方向及过程,直到剩下二只时,以排到后面的(指报数方向)为大王。
   ③ N 只猴子围成一圈,从第 P 个开始,每隔 M 只报数,打印每次过程,只剩下一个时为大王。

[C#]

① and ③ 解法1
        public const int N = 20;
        public const int M = 4;
        public const int Start = 1;//1~N

        public static void Main(String[] args)
        {
            int[] old = new int[N + 1];
            int[] Result = new int[N + 1];
            int no=0;

            int i,j,k;
            for (i = 1; i <= N; i++)
                old[i] = i;

            int start = Start;
            for (i = N; i >= 2; i--)
            {
                start = (start + M-1) % i;
                if (start == 0) start = i;
                Result[no++] = old[start];

                for (j = start; j <= i - 1; j++)
                    old[j] = old[j + 1];
            }
            Result[no] = old[1];

            for (k = 0; k < N; k++)
                Console.Write("{0,3}", Result[k]);
            Console.Write("/n");
           Console.ReadLine();
        }


① and ③ 解法2
         public const int N = 20;
        public const int M = 4;
        public const int Start = 4;//1~N

        public static void Main(String[] args)
        {
            char[] mon = new char[N];
            int now, i,no;
            no = 0;
            now = Start-1;
            i = 1;

            while (no != N)
            {
                if (mon[now] != '#')
                    if (i != M)
                    {
                        now = ++now % N;
                        i++;
                    }
                    else
                    {
                        mon[now] = '#';
                        no++;
                        Console.WriteLine("{0}号猴子出局。", now + 1);
                        i = 1;
                        now = ++now % N;
                    }
                else
                    now = ++now % N;
            }

            if (now == 0) now = N;
            Console.WriteLine("{0}号猴子是大王!", now);           

            Console.ReadLine();
        }

   ②
        public const int N = 20;
        public const int M = 4;
        public const int Start = 1;//1~N

        public static void Main(String[] args)
        {
            char[] mon = new char[N];
            int now, i,j,no,max=N-1,min=0;
            no = 0;
            now = Start-1;
            i = 1;
            bool b1 = true;

            while (no < N-2)
            {
                if (mon[now] != '#')
                    if (i != M)
                    {                       
                        if (b1)
                            ++now;
                        else
                            --now;
                        if (now == max)
                            b1 = false;
                        if (now == min)
                            b1 = true;                       
                        i++;
                    }
                    else
                    {
                        mon[now] = '#';

                        for (j = N - 1; j >= 0; j--)
                            if (mon[j] != '#') break;
                        max = j;
                        for (j = 0; j <N; j++)
                            if (mon[j] != '#') break;
                        min = j;

                        no++;
                        Console.WriteLine("{0}: {1}号猴子出局。", no,now + 1);
                        i = 1;

                        if (b1)
                            ++now;
                        else
                            --now;
                        if (now == max)
                            b1 = false;
                        if (now == min)
                            b1 = true;                      
                    }
                else
                {
                    if (b1)
                        ++now;
                    else
                        --now;
                    if (now == max)
                        b1 = false;
                    if (now == min)
                        b1 = true;                   
                }
            }
           
            Console.WriteLine("{0}: {1}号猴子是大王!",b1,b1?max+1:min+1);      

            Console.ReadLine();
        }

 

 

[C++] 

            #include<iostream>
            #include<stdlib.h>
            using namespace std;
            #define M 20//共有M个猴子。
            #define N 4//数到N者出局。
            void main()
            {
                int i,l,p,t;
                char mon[M];//例只猴子。
                i=1;
                l=0;
                p=0;//当前元素。
                while(l!=M)
                {
                    if(mon[p]!='#')
                        if(i!=N)
                        {
                            i++;
                            p=++p%M;
                        }
                        else
                        {
                            mon[p]='#';
                            t=p;
                            cout<<t+1<<"号猴子出局。"<<endl;
                            p=++p%M;
                            l++;
                            i=1;
                        }
                    else
                        p=++p%M;


                };
                cout<<t+1<<"号猴子是大王!"<<endl;

          system("pause");
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值