64,682
社区成员
发帖
与我相关
我的任务
分享
while (1) {
cout << "输入数据(4位数字):";
cin >> str2;
//cout<<str2<<endl;
if (strlen(str2)!=4) continue;
if (!(isdigit(str2[0])
&& isdigit(str2[1])
&& isdigit(str2[2])
&& isdigit(str2[3]))) continue;
int x[4],y;
y=0;
for (n = 0; n < 4; n++){
x[n]=0;
if (str1[n] == str2[n]){
x[n]=n+1;
y++;
}
}
if (y == 4){
cout << "恭喜你答对了!" << endl;
getchar();
return 0;
};
cout << "恭喜你答对了" << y << "位位置" << endl;
y=0;
for (n=0;n<4;n++) {
for (m=0;m<4;m++) {
if (str2[n]==str1[m]) {
y++;
break;
}
}
}
cout << "恭喜你答对了" << y << "位数字" << endl;
}
void CheckInput(int input[],int target[])
{
int A=0,B=0,i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(input[i]==target[j]) B++;
}
}
for(i=0;i<4;i++)
{
if(input[i]==target[i])
{
A++;
B--;
}
}
printf("%dA%dB\n",A,B);
}
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <ctime>
#include <Windows.h>
using namespace std;
#define MAX 10
#define N 4
int main()
{
int i, j, k, n, m;
int a[N];
char str1[N], str2[N + 1];
//产生随机数
srand((unsigned)time(NULL));//srand()函数产生一个以当前时间开始的随机种子
for (n = 0; n<4; n++)
{
a[n] = rand() % MAX;//MAX为最大值,其随机域为0~MAX-1
//cout << a[n];
str1[n] = a[n] + '0';
}
int flag = 1;
while (flag>0)
{
flag++;
cout << "输入数据:";
cin >> str2;
//cout<<str2<<endl;
int x[6] = { 0 }, y = 0;
for (n = 0; n < 4; n++){
if (str1[n] == str2[n]){
x[n]=n+1;
y++;
};
};
//for (i = 0; i < 4; i++){
// for (n = 0; n < 4; n++){
// if (str1[i] == str2[n] && str1[n] != str2[n])
// y++;
// };
//
//};
if (y == 4){
cout << "恭喜你答对了!" << endl;
flag = 0;
getchar();
exit;
};
for (int i = 0; i < 4;i++){
cout << "恭喜你答对了" << x[n]<<"位置"<<endl;
}
cout << "恭喜你答对了" << y << "位" << endl;
y = 0;
for (int i = 0; i < 4;i++){
x[n] = 0;
};
}
}