64,637
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define a 10
#define b 20
unsigned char m[a][b];
int p;//0..100 在m[a][b]中1所占比例%
int i,j;
int main() {
srand(time(NULL));
while (1) {
printf("Input percentage of 1 in m[%d][%d](0..100):",a,b);
rewind(stdin);
if (1==scanf("%d",&p)) {
if (0<=p && p<=100) break;
}
}
for (i=0;i<a;i++) {
for (j=0;j<b;j++) {
m[i][j]=((rand()%100)<p)?1:0;
printf("%d ",m[i][j]);
}
printf("\n");
}
return 0;
}