C/C#双色球

6个红色球号码和1个蓝色球
红色球号码从1—33中选择
蓝色球号码从1—16中选择

C:(粗体:随机数,红色:库排序)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int blue[6];void printArr(int arr[],int len );
bool checkNum(int arr[],int len, int now);
int compare(const void *value1, const void *value2);
main() {
    srand(time(0));
    for(int i = 0; i< 6; i++) {
        int n = rand()%33+1;
        if (!checkNum(blue, 6, n)) {
            i--;
            continue;
        }
        blue[i] = n;
    }
    printf("取到的蓝球:");
    printArr(blue, 6);

    printf("排序后蓝球:");
    // 函数库自带的快速排序函数
    // 对于有多个重复值的数组来说,效率较低不稳定
    qsort(blue, 6, sizeof(int), compare);

    printArr(blue, 6);

    printf("红球:%d", rand()%16+1);
}
// qsort 要结合  compare使用
int compare(const void *value1, const void *value2) {
    // 升序
    return *(int*)value1 - *(int*)value2;
}

void printArr(int arr[],int len ) {
    for(int i = 0 ; i<len; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
bool checkNum(int arr[],int len, int now) {
    for(int i = 0 ; i<len; i++) {
        if (arr[i] == now) {
            return false;
        }
    }
    return true;
}

 

C#

using System;

namespace ConsoleApplication2 {
    class Program {
        static int[] 红球 = new int[6];
        static void Main(string[] args) {
            Random r = new Random();
            for (int i = 0; i < 6; i++)
            {
                int n = r.Next(33) + 1;
                if (重复Check(n))
                {
                    i--;
                    continue;
                }
                红球[i] = n;
            }
            Array.Sort(红球);
            foreach (int n in 红球)
            {
                Console.WriteLine(n);
            }
            // 蓝球
            Console.WriteLine("蓝球:" + (r.Next(16) + 1));
            Console.ReadKey();
        }
        static bool 重复Check(int now) {
            foreach (int n in 红球)
            {
                if (now == n)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

 

转载于:https://www.cnblogs.com/AndyHoo/p/6391100.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值