#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注。现在有这样一个要求,从这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 关注
- 911 浏览
添加回答
举报
0/150
提交
取消
