#include
#include
using namespace std;
int c[10]={0,1,2,3,4,5,6,7,8,9};
int index(int* a,int x){
int i;
for(i=0;i<4;i++)
if(a[i] == x) return i;
return -1;
}
void test(int* a, int* b,int* r){
int aa[4];
int bb[4];
int i;
for(i=0;i<4;i++){
aa[i] = a[i];
bb[i] = b[i];
}
int rp=0;
int rn=0;
for(i=0; i<4; i++){
if(aa[i]==bb[i]) {
rp ++;
aa[i] = bb[i] = 0;
}
}
int idx = -1;
for(i=0;i<4;i++) {
if(bb[i]==0) continue;
idx = index(aa, bb[i]);
if(idx >=0){
rn ++;
aa[idx]=0;
}
}
r[0] = rn;
r[1] = rp;
return;
}
void my_rand(int a[],int n){
int i,j,k;
for(i=0;ik= rand()%(10-i);
int jj =0;
for(j=0;jif(c[jj]>=0){
j++;
}
jj++;
}
a[i] = c[jj];
c[jj] = -1;
}
for(i=0;i<10;i++)
c[i] = i;
}
void my_print(int a[],int n){
int t=0;
int i;
for(i=0;i<4;i++){
t = t*10 + a[i];
}
cout << t ;
cout << endl;
}
void my_input(int a[],int n){
cout << "请输入4位不重复的数字的数字:" << endl;
int t;
cin >> t;
int i=n-1;
while(i>=0){
a[i] =t%10;
i--;
t = t/10;
}
}
int my_equal(){
}
void see_more(int a[],int b[],int n){
int i;
for(i=0;iif(a[i] == b[i]){
cout << "第" << i+1 << "位正确"<< endl;
}
else{
cout << "第" << i+1 << "位不正确"<< endl;
}
}
}
void see_ans(int a[],int n){
int pwd = 123456;
int p;
cout << "请输入密码"<< endl;
while(1){
cin >> p;
if(p==pwd){
break;
}
else{
cout << "请输入密码"<< endl;
}
}
my_print(a,n);
}
void my_copy(int a[],int d[],int n){
int i;
for(i=0;id[i] = a[i];
}
int main(){
int a[4];
int b[4];
int d[4];
int r[2];
int score;
int t;
score = 0;
int a8,a7,a6;
int start = 1;
while(1){
if(start == 1){
my_rand(a,4);
}
my_input(b,4);
a8 = (b[0]==8 && b[1]==8 && b[2] ==8 && b[3] == 8);
a7 = (b[0]==7 && b[1]==7 && b[2] ==7 && b[3] == 7);
a6 = (b[0]==6 && b[1]==6 && b[2] ==6 && b[3] == 6);
if(!a8) my_copy(b,d,4);
if(a8){
see_more(a,d,4);
}
else if(a7){
see_ans(a,4);
}
else if(a6){
cout << "你的得分为:" << score << endl;
ofstream out;
out.open("output.txt");
out << score << endl;
out.close();
system("pause");
return 0;
}else{
start = 0;
test(a,b,r);
if(r[1] == 4){
score += 40;
cout << "恭喜你,你猜对了!!!" << endl;
cout << "请输入6666退出或者继续游戏" << endl;
start = 1;
}
else {
cout << r[1]<<" " << r[0]<< endl;
score -= 20;
}
}
}
}
稍微修改下就行了~~~加油~~~
上个星期刚刚花10分钟写了一个。。。。自己无聊玩玩还行
发私信给你了