亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

幫忙算算,C語言,隨機,從固定結果里面機選一個號碼。

幫忙算算,C語言,隨機,從固定結果里面機選一個號碼。

C++
心有法竹 2018-07-17 11:23:56
#include "stdafx.h"#includeint main(){int a, b, c, d, e, i = 0;for (a = 1; a <= 15; a++)for (b = 1; b <= 15; b++)for (c = 1; c <= 15; c++)for (d = 1; d <= 15; d++)for (e = 1; e <= 15; e++)if (b - a > 0 && c - b > 0 && d - c > 0 && e - d > 0){printf("A:%2d B:%2d C:%2d D:%2d E:%2d ", a, b, c, d, e);i++;if (i % 1 == 0)printf("\n");}printf("一共%d種\n", i);return 0;}這一段代碼運行結果:從15個球里面選5個,一共有3003注?,F在有這樣一個要求,從這3003注號碼里面機選10注打印出來,這個代碼該如何添加要求:1,用C語言。2,在源代碼的基礎上添加或者修改。謝謝老鐵。
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var all = seedGen(1, 15, 5)
                .Select(x => new { a = x[0], b = x[1], c = x[2], d = x[3], e = x[4] }).Where(x => x.b > x.a && x.c > x.b && x.d > x.c && x.e > x.d).ToList();
            Console.WriteLine(all.Count);
            Console.WriteLine("random 10");
            foreach (var item in all.OrderBy(_ => Guid.NewGuid()).Take(10))
            {
                Console.WriteLine(item);
            }
        }

        static IEnumerable<int[]> seedGen(int lb, int ub, int n)
        { 
            var seed = Enumerable.Range(lb, ub - lb + 1).Select(x => new int[] { x });
            for (int i = 1; i < n; i++) seed = seed.SelectMany(x => Enumerable.Range(lb, ub - lb + 1).Where(y => !x.Contains(y)).Select(y => x.Concat(new int[] { y }).ToArray()));
            return seed;
        }
    }
}

https://img1.sycdn.imooc.com//5b5ad0c40001f73a00010015.jpg

看這個程序,簡潔不簡潔。
.Where(x => x.b > x.a && x.c > x.b && x.d > x.c && x.e > x.d)
這里可以改變你的條件

seedGen(1, 15, 5) 這里是從1~15,選5個。

Take(10) 隨機選10條


查看完整回答
反對 回復 2018-07-27
  • 1 回答
  • 0 關注
  • 886 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號