题目:模拟一种 猜数字 游戏
分析:模拟
注意:不要把 数对且位置对 与 数对且位置错误 重复计算 ps:我这个程序数字是从1-9(不是0-9)因为我把0作为一个标志。所以玩游戏的话,可能要改进。
#include <iostream> #include"cstring" #include"cstdio" using namespace std; void set_tempcode(int *tempcode,int *code,int n); int main() { int n; int code[1000],guess[1000]; int a,b; int k=0;//记录game while(scanf("%d",&n)&&n)//code几个数 { for(int i=0; i<n; i++) scanf("%d",&code[i]);//code k++;//记录游戏次数 int counter=0;//记录guess次数 while(1)//guess { a=0; b=0; counter++;//初始化 for(int i=0; i<n; i++) { scanf("%d",&guess[i]); } if(guess[0]==0) break; int tempcode[1000]; set_tempcode( tempcode,code,n);//初始化 //同位置 for(int i=0; i<n; i++) { if(tempcode[i]==guess[i]) { a++; tempcode[i]=0; guess[i]=0; } } //不同位置 同数 for(int i=0; i<n; i++) { if(tempcode[i]==0) continue; for(int j=0; j<n; j++) { if(guess[j]==0) continue; else if(guess[j]==tempcode[i]) { b++; guess[j]=0; tempcode[i]=0; } } } //输出 if(counter==1) printf("Game %d:\n",k); printf(" (%d,%d)\n",a,b); } } return 0; } void set_tempcode(int *tempcode,int *code,int n) { for(int i=0; i<n; i++) { tempcode[i]=code[i]; } return ; }