给你一个框架,有几段代码还是要你自己实现的。
#include <stdio.h>
#define K 6
#define B 9
#define MAX_TIME 6
void initNum(int * sourceNum, int n)
{
//简易代码,建议用rand随机数生成1-B的数字
for(int i=0;i<n;i++) sourceNum[i]=i+1;
}
int inputNum(int * sourceNum, int n)
{
for(int i=0;i<n;i++) scanf("%d", sourceNum+i);
return 1;
}
int checkValid(int * userNum, int n)
{
//简易代码,检查数字是否符合范围要求
return 1;
}
int checkAllRight(int * sourceNum, int * userNum, int
n)
{
int res=0;
for(int i=0;i<n;i++)
{
if(sourceNum[i] == userNum[i])
{
printf("%c
", 2);
res++;
}
}
printf("\n");
return res;
}
void checkPartRight(int * sourceNum, int * userNum, int
n)
{
//简易代码,统计数值对,位置错的个数
for(int i=0;i<n;i++)
{
printf("%c
", 1);
}
printf("\n");
}
int main ()
{
int time, result, allRightNumber;
int sourceNum[K], userNum[K];
time = MAX_TIME;
result = 0;
initNum(sourceNum, K);
while((--time)&&(!result))
{
printf("Please input %d numbers(1~%d):
%d times left\n", K, B, time);
while(inputNum(userNum, K)&&!checkValid(userNum, K));
checkPartRight(sourceNum, userNum, K);
allRightNumber = checkAllRight(sourceNum, userNum, K);
if(allRightNumber == K) result = 1;
}
if(result)
{
printf("Good job!\n");
}
else
{
printf("Time out! The numbers are: ");
for(int i=0; i<K; i++) printf("%d
", sourceNum[i]);
printf("\n");
}
return 0;
}