#include "stdafx.h"
#include
int 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,在源代码的基础上添加或者修改。
谢谢老铁。
定义数组保存结果,循环完成随机取10组就行
#include "stdio.h"
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int randint(int m){
return (int)(1.0 * m * rand()/(RAND_MAX+1.0));//將随机数控制在0~m-1之间
}
int main() {
int a, b, c, d, e, i = 0;
int poses[] = {0, 0, 0, 0, 0};
srand( (int)time(0) );
const int TOTAL = 3003; //计算知道为这么多
int idx = 0;
while (idx < sizeof(poses)/sizeof(int)) {
poses[idx] = randint(TOTAL);
bool isValOK = true;
for (int j = 0; j < idx; j++) {
if (poses[idx] == poses[j]) {
isValOK = false;
break;
}
}
if (isValOK) idx++;
}
for (int j = 0; j < sizeof(poses)/sizeof(int); j++) printf("%2d ", poses[j]);
printf("\n");
for (a = 1; a <= 15; a++)
for (b = a+1; b <= 15; b++)
for (c = b+1; c <= 15; c++)
for (d = c+1; d <= 15; d++)
for (e = d+1; e <= 15; e++)
{
i++;
for (int j = 0; j < sizeof(poses)/sizeof(int); j++)
if (poses[j] == i)
printf("A:%2d B:%2d C:%2d D:%2d E:%2d\n", a, b, c, d, e);
}
printf("一共%d种\n", i);
return 0;
}
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条
还是这个程序,你需要别的条件,改下where的,比如a+b+c+d+e=48,看下,也马上出结果。
总之你不用管别的地方,只要修改where条件,这个程序几乎是万能的。你要10选5,20选6,等等,也可以简单修改。
再来一个存文件的,也是1行代码。
当然,如果你要在你的程序上修改,也可以。采纳了帮你写。
File.WriteAllLines("1.txt", ten.Select(x => x.ToString()).ToArray());
{ a = 2, b = 3, c = 5, d = 9, e = 12 }
{ a = 2, b = 9, c = 11, d = 12, e = 15 }
{ a = 3, b = 7, c = 11, d = 14, e = 15 }
{ a = 2, b = 10, c = 11, d = 13, e = 15 }
{ a = 3, b = 6, c = 8, d = 10, e = 11 }
{ a = 1, b = 6, c = 11, d = 13, e = 14 }
{ a = 4, b = 5, c = 10, d = 11, e = 13 }
{ a = 1, b = 8, c = 10, d = 11, e = 12 }
{ a = 3, b = 5, c = 11, d = 12, e = 15 }
{ a = 2, b = 4, c = 6, d = 8, e = 11 }