C#:今日上机问题描述:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2-10为数字本身,A为1,J为11,Q为12,K为13,而大小王可以看成任意数字。

using System;
//写的不太严格,直接用14张牌来抽了,1-13,大小王为0,没有判断相同的不能超过4张,为0的相同不能超过两张。
namespace _201919144122WeiXu
{
    class Program
    {
        static void Main(string[] args)

        {
            int[] a = new int[5];
            int count = 0;
            Random random = new Random();
            for(int i = 0;i <= 4;i++)
            {
                a[i] = random.Next(0,13);
            }
            for(int i = 0;i <= 4;i++)
            {
              Console.Write(a[i]+ " ");
               
            } 
            Console.Write("\n");
            for (int i = 0; i <= 4; i++)
            {
                for (int j = 0; j < 5 - i - 1; j++)
                {
                    if (a[j] > a[j + 1])
                    {
                        int b = 0;
                        b = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = b;
                    }
                }
            }
            for (int i = 0; i < 5; i++)
            {
                Console.Write(a[i] + " ");
            }


            for (int i = 0; i <=4; i++)
            {
                for (int j = i + 1; j <=4; j++)
                {
                    if ((a[i] == a[j]) && (a[i]!= 0) && a[j] != 0)
                    {
                        Console.WriteLine("不是顺子\n");
                        return;
                    }

                }
            }  
           
            for (int i = 0; i < 5; i++)
            {
                if (a[i] == 0)
                    count++;
            }
            if(count ==0)
            {
                if(a[4]-a[0]==4)
                {
                    Console.WriteLine("为顺子");
                }
                else
                {
                    Console.WriteLine("不是顺子");
                }
                return;

            }
            if (count == 1)
            {
                if (a[4] - a[1] == 4 || a[4] - a[1]==3)
                {
                    Console.WriteLine("为顺子");
                }
                else
                {
                    Console.WriteLine("不是顺子");
                }
                return;

            }
            if (count == 2)
            {
                if (a[4] - a[2] == 4 || a[4] - a[2] == 3 || a[4] - a[2] == 2)
                {
                    Console.WriteLine("为顺子");
                }
                else
                {
                    Console.WriteLine("不是顺子");
                    
                }
                return;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值