1.猜随机数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
// 产生四个随机数放在一个数组中
int num = 0;
int rand_num[4];
int guess_num[4];
srand(time(NULL));
for(int i = 0;i<4;i++)
{
rand[num] = rand()%10;
}
//
printf(请输入一个四位数:\n");
scanf("%d",&num);
guess_num[0] = (num/1000)%10;
guess_num[1] = (num/100)%10;
guess_num[2] = (num/10)%10;
guess_num[3] = num % 10;
n = 0;
while(1)
{
for(int j = 0; j < 4; j++)
{
if(guess_num[j] > rand_num[j])
{
printf("第%d位大于正确数字\n",j+1);
}
else if (guess_num[j] < rand_num[j])
{
printf("第%d位大于正确数字\n",j+1);
}
else
{
printf("第%d位正确数字\n");
n++
}
}
if(n == 4){
printf("恭喜你,猜对了!\n");
sleep(2);
return 0; // 结束程序
}
}
}