#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;
}
}
}
看這個程序,簡潔不簡潔。
.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條
- 1 回答
- 0 關注
- 894 瀏覽
添加回答
舉報
0/150
提交
取消
