鱼C论坛

 找回密码
 立即注册
查看: 1233|回复: 3

[已解决]数组中随机数的问题

[复制链接]
发表于 2020-12-9 23:41:37 | 显示全部楼层 |阅读模式

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

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

x
想在一个二维数组中随机赋值1和0。比如说在6*6的二维数组中随机赋值三个1,其他都为0,没有思路
最佳答案
2020-12-10 10:59:51
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. #define        MAX_COL        (6)
  5. #define MAX_ROW        (6)
  6. #define CNT_1        (3)

  7. int main()
  8. {        
  9.         int array[MAX_COL][MAX_ROW] = {0};
  10.         int index = 0;
  11.         int count = 0;
  12.         int col = 0;
  13.         int row = 0;
  14.        
  15.         srand(time(NULL));

  16.         do
  17.         {
  18.                 index = rand() % (MAX_COL * MAX_ROW);
  19.                 col = index / MAX_COL;
  20.                 row = index % MAX_ROW;

  21.                 if(array[col][row] == 0)
  22.                 {
  23.                         array[col][row] = 1;
  24.                         count++;
  25.                 }

  26.                 if(count >= CNT_1)
  27.                 {
  28.                         break;
  29.                 }
  30.         }while(1);

  31.         for(col=0 ; col<MAX_COL ; col++)
  32.         {
  33.                 for(row=0 ; row<MAX_ROW ; row++)
  34.                 {
  35.                         printf("%d,",array[col][row]);
  36.                 }
  37.                 printf("%\n");
  38.         }

  39.         return 0;        
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-9 23:51:14 | 显示全部楼层
数组中原来有值吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-10 10:59:51 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. #define        MAX_COL        (6)
  5. #define MAX_ROW        (6)
  6. #define CNT_1        (3)

  7. int main()
  8. {        
  9.         int array[MAX_COL][MAX_ROW] = {0};
  10.         int index = 0;
  11.         int count = 0;
  12.         int col = 0;
  13.         int row = 0;
  14.        
  15.         srand(time(NULL));

  16.         do
  17.         {
  18.                 index = rand() % (MAX_COL * MAX_ROW);
  19.                 col = index / MAX_COL;
  20.                 row = index % MAX_ROW;

  21.                 if(array[col][row] == 0)
  22.                 {
  23.                         array[col][row] = 1;
  24.                         count++;
  25.                 }

  26.                 if(count >= CNT_1)
  27.                 {
  28.                         break;
  29.                 }
  30.         }while(1);

  31.         for(col=0 ; col<MAX_COL ; col++)
  32.         {
  33.                 for(row=0 ; row<MAX_ROW ; row++)
  34.                 {
  35.                         printf("%d,",array[col][row]);
  36.                 }
  37.                 printf("%\n");
  38.         }

  39.         return 0;        
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-10 16:44:56 | 显示全部楼层

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-5 02:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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