鱼C论坛

 找回密码
 立即注册
查看: 771|回复: 2

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

[复制链接]
发表于 2022-2-26 15:27:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 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 16:08:52
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 6

  5. void r(int *blue){
  6.     for(int i = 0; i < N; i++) blue[i] = rand() %33 + 1;
  7. }

  8. int check(int *blue){
  9.     for(int i = 0; i < N-1; i++)
  10.     for(int j = i+1; j < N; j++)
  11.     if(blue[i] == blue[j])
  12.     return 0;
  13.     return 1;
  14. }

  15. int main()
  16. {
  17.     srand(time(NULL));
  18.     int blue[6], red = rand() %16 + 1;
  19.     while(1){
  20.         r(blue);
  21.         if(check(blue)) break;
  22.     }
  23.     printf("blue balls: ");
  24.     for(int i = 0; i < N; i++) printf("%d ", blue[i]);
  25.     printf("\nred  balls: %d\n", red);

  26.     return 0;
  27. }
复制代码
  1. blue balls: 15 10 28 21 4 19
  2. red  balls: 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-26 15:58:30 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdbool.h>

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

  10. bool verify(int data[], size_t size) {
  11.     for(size_t i = 0; i < size; ++i) {
  12.         for(size_t j = i + 1; j < size; ++j) {
  13.             if(data[i] == data[j]) return false;
  14.         }
  15.     }
  16.     return true;
  17. }

  18. int main() {
  19.     srand(time(NULL));
  20.     int red_ball[6];
  21.     int blue_ball;
  22.     for(size_t i = 0; i < 10; i++) {
  23.         while(true) {
  24.             random_set(red_ball, 6);
  25.             if(verify(red_ball, 6)) break;
  26.         }
  27.         blue_ball = rand() % 16 + 1;
  28.         printf("******************************************************************************\n");
  29.         printf("第%lu组号码:\n", i + 1);
  30.         printf("红球号码为:");
  31.         for(size_t i = 0; i < 6; ++i) {
  32.             printf("\t%d", red_ball[i]);
  33.         }
  34.         puts("");
  35.         printf("篮球号码为:\t%d\n", blue_ball);
  36.         printf("******************************************************************************\n");
  37.     }
  38.     return 0;
  39. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-26 16:08:52 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 6

  5. void r(int *blue){
  6.     for(int i = 0; i < N; i++) blue[i] = rand() %33 + 1;
  7. }

  8. int check(int *blue){
  9.     for(int i = 0; i < N-1; i++)
  10.     for(int j = i+1; j < N; j++)
  11.     if(blue[i] == blue[j])
  12.     return 0;
  13.     return 1;
  14. }

  15. int main()
  16. {
  17.     srand(time(NULL));
  18.     int blue[6], red = rand() %16 + 1;
  19.     while(1){
  20.         r(blue);
  21.         if(check(blue)) break;
  22.     }
  23.     printf("blue balls: ");
  24.     for(int i = 0; i < N; i++) printf("%d ", blue[i]);
  25.     printf("\nred  balls: %d\n", red);

  26.     return 0;
  27. }
复制代码
  1. blue balls: 15 10 28 21 4 19
  2. red  balls: 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 01:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表