CSDN博客

img avalonBBS

猜数字的程序

发表于2004/10/26 14:56:00  1611人阅读

/*
  Author:   avalon    qq:1243128 
  Date:        26-10-04 14:51
  Description: 0-9中随即抽选4个数,组成4位数,(这次仅讨论他们不重复的情况.)
               然后由程序猜这个数值,人给出猜的结果,
               用xAyB表示,A表示你猜对,且这个数值的位置也正确的有X个,
               B表示你猜对,但是位置的错误的数值有Y个.
               这是一个用筛选法做的程序。。算法不太好的说:(
*/
#include <iostream>
#include <list>
#include <vector>
#include <string>
#include <stdlib.h>
using namespace std;
//全局VECTOR,用于存储数字集合
list<string> number;
void init(void)
{/////////////初始化VECTOR///////////////
    for(int a=0;a<=9;a++){
        for(int b=0;b<=9;b++){
            if(a==b)continue;
            for(int c=0;c<=9;c++){
                if(c==a || c==b)continue;
                for(int d=0;d<=9;d++){
                    if(d!=a && d!=b && d!=c){
                        char sa[4]={a+'0',b+'0',c+'0',d+'0'};
                        string temp(sa,sa+4);                                                                  
                        number.push_back(temp);
                    }   
         }  }//c end
    }  }//a end
}//fun end
bool comp(string ss,string sd,string input)
{//比较!!如果两个数不符合条件input,则返回false
 //num_s为vector中的数,num_d为猜测数,input为串
  int s[4],d[4];
  if(ss == sd)return false;
  //分解 
  s[0]=ss[0]-'0';s[1]=ss[1]-'0';s[2]=ss[2]-'0';s[3]=ss[3]-'0';
  d[0]=sd[0]-'0';d[1]=sd[1]-'0';d[2]=sd[2]-'0';d[3]=sd[3]-'0';
  //xAyB
    int x=input[0]-'0';   
    int y=input[2]-'0';   
    //comp........
    do{
        int xy=x+y;//共匹配的数        
        int sum=0;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(s[i]==d[j])sum++;
            }
        }
        if(sum!=xy)return false;
        //           
    }while(0);
    do{
        //绝对匹配x  
        if(x){
            int sum=0;
            for(int i=0;i<4;i++){
                if(s[i]==d[i])sum++;
            }
            if(sum!=x)return false;        
        }    
    }while(0);
    do{//仅数字匹配,位置不匹配
        if(y){
            int sum=0;
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    if(s[i]==d[j] && i!=j)sum++;
                }
            }
            if(sum!=y)return false;
        }           
    }while(0);                                               
 return true;
}                        
void filt(string num,string input)
{//过滤!! num为猜测数,input为人输入的串
 list<string>::iterator iter=number.begin();
  while(iter!=number.end()){
      list<string>::iterator temp=iter;
      iter++;
      //
      if( comp(*temp,num,input)==false )number.erase(temp);
   }      
}
string sui_ji(void)
{//在vector中随机选个数
    vector<string> temp(number.begin(),number.end());
 random_shuffle(temp.begin(),temp.end()); 
  return *(temp.begin());
}                  
int main(void)
{
    string quit;  
    do{
        init();
       do{
           string input;
           string num;
           cout<<"输入avalon或者AVALON或者4a4b或者4A4B结束!"<<endl;
           while( 1){
               num=sui_ji();
               cout<<num<<'/t'<<"数据集还有"<<number.size()<<"个数"<<endl;
               cin>>input;
               if( input=="avalon" || input=="AVALON"){
                   list<string>::iterator iter=number.begin();
                   int enter=0;
                   for(;iter!=number.end();iter++){                      
                       if(enter++%8==0)cout<<endl;                      
                       cout<<*iter<<"    ";
                   }
                   cout<<endl;
                   break;   
               }
               if( input == "4a0b" || input =="4A0B"){
                   cout<<"ok"<<endl;
                   break;
               }   
               filt(num,input);
           }   
       }while(0);
       //清空vector   
       number.erase(number.begin(),number.end());
      
       cout<<"继续吗?";
       cin>>quit;       
   }while(quit=="y" || quit=="Y");
   cout<<"bye~~~~~~~~~~~~~~~~~"<<endl;        
  system("PAUSE"); 
  return 0;
}
阅读全文
0 0

相关文章推荐

img
取 消
img