C言語 重複しないランダム数字作成
仕様:qを入力するとプログラムを終了する、ランダム数字重複しないで作成
#include
#include
#include
#define MAX 100
int main(void)
{
int i, j, flag, num, a[MAX] = { 0 }, max, ch;
srand((unsigned)time(NULL));
printf(“Please input max number<1-39>(q to quit): “);
a:while ((scanf(“%d", &max)) == 1)
{
if (max >= 40)
{
printf(“Please input max number<1-39>(q to quit): “);
while (getchar() != '\n’)
continue;
continue;
}
for (i = 0; i < max; ++i)
{
do{num = rand() % 41 + 1;
}while(num==8||num==36);
flag = 1;
for (j = 0; j < i; ++j)
{
if (num == a[j] || num == 8 || num == 36)
{
flag = 0;
break;
}
}
if (flag)
a[i] = num;
else
--i;
}
while
(getchar() != '\n') continue;
for (i = 0; i < max; ++i)
printf("%d ", a[i]);
printf("\n");
printf("Please input max number<1-39>(q to quit): “);
}
if (getchar() != 'q’)
{
puts(“q to quit,please!");
printf(“Please input max number<1-39>(q to quit): “);
while (getchar() != '\n’)
continue;
goto a;
}
printf(“Bye!");
return 0;
}