18227452746 发表于 2022-2-26 15:27:29

各位大神,可以帮我排序 && 简化一下代码吗?

本帖最后由 18227452746 于 2022-2-26 15:29 编辑

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
int main()
{
srand(time(NULL));
int hq1,hq2,hq3,hq4,hq5,hq6;
int lq;
int i;
for(i=0;i<10;i++)
{
    MAA:
    hq1 = rand()%33+1;
    hq2 = rand()%33+1;
    hq3 = rand()%33+1;
    hq4 = rand()%33+1;
    hq5 = rand()%33+1;
    hq6 = rand()%33+1;
    lq = rand()%16+1;

    if(hq1==hq2 || hq1==hq3 || hq1==hq4 || hq1==hq5 || hq1==hq6 || hq2==hq3 || hq2==hq4 || hq2==hq5 || hq2==hq6 || hq3==hq4 || hq3==hq5 || hq3==hq6 || hq4==hq5 || hq4==hq6 || hq5==hq6 )
      {
            goto MAA;
      }
    else{
            printf("******************************************************************************\n");
            printf("第%d组号码:\n",i+1);
            printf("红球号码为:\t%d\t%d\t%d\t%d\t%d\t%d\n篮球号码为:\t%d\n",hq1,hq2,hq3,hq4,hq5,hq6,lq);
            printf("******************************************************************************\n");
            Sleep(500);
    }
    }
    getchar();
return 0;
}

人造人 发表于 2022-2-26 15:58:30

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

void random_set(int data[], size_t size) {
    for(size_t i = 0; i < size; ++i) {
      data = rand() % 33 + 1;
    }
}

bool verify(int data[], size_t size) {
    for(size_t i = 0; i < size; ++i) {
      for(size_t j = i + 1; j < size; ++j) {
            if(data == data) return false;
      }
    }
    return true;
}

int main() {
    srand(time(NULL));
    int red_ball;
    int blue_ball;
    for(size_t i = 0; i < 10; i++) {
      while(true) {
            random_set(red_ball, 6);
            if(verify(red_ball, 6)) break;
      }
      blue_ball = rand() % 16 + 1;
      printf("******************************************************************************\n");
      printf("第%lu组号码:\n", i + 1);
      printf("红球号码为:");
      for(size_t i = 0; i < 6; ++i) {
            printf("\t%d", red_ball);
      }
      puts("");
      printf("篮球号码为:\t%d\n", blue_ball);
      printf("******************************************************************************\n");
    }
    return 0;
}

傻眼貓咪 发表于 2022-2-26 16:08:52

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 6

void r(int *blue){
    for(int i = 0; i < N; i++) blue = rand() %33 + 1;
}

int check(int *blue){
    for(int i = 0; i < N-1; i++)
    for(int j = i+1; j < N; j++)
    if(blue == blue)
    return 0;
    return 1;
}

int main()
{
    srand(time(NULL));
    int blue, red = rand() %16 + 1;
    while(1){
      r(blue);
      if(check(blue)) break;
    }
    printf("blue balls: ");
    for(int i = 0; i < N; i++) printf("%d ", blue);
    printf("\nredballs: %d\n", red);

    return 0;
}blue balls: 15 10 28 21 4 19
redballs: 1
页: [1]
查看完整版本: 各位大神,可以帮我排序 && 简化一下代码吗?