我想创建一个生成随机数的程序,然后要求用户输入已生成的素数的数量。如果该数字正确,则该用户被宣布为赢家,否则将输。请帮忙。
试试这个代码-
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int arr[10], i, j, n, count=0;
srand(getpid()); //Every time it will give the different seed to rand(), so you wont get same array again and again
for (i = 0; i < 10; i++)
arr[i] = (rand()%100) + 1;
printf("Enter the No of prime numbers!\n");
scanf("%d", &n);
for (i = 0; i < 10; i++) {
for (j = 2; j < arr[i]; j++)
if (arr[i]%j == 0)
break;
if (arr[i] == j)
count++;
}
printf("Prime Num in Array = %d, User I/p = %d\n",count,n);
if(count == n)
printf("Yeah! You have guessed correctly!\n");
else
printf("Sorry! Try again!\n");
return 0;
}
样品输出
root@ubuntu:~/c/basics# ./a.out
Enter the No of prime numbers!
3
Prime Num in Array = 1, User I/p = 3
Sorry! Try again!
root@ubuntu:~/c/basics# ./a.out
Enter the No of prime numbers!
2
Prime Num in Array = 2, User I/p = 2
Yeah! You have guessed correctly!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句