#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <ctime>
const int wei=4;
using namespace std;
void num2array(int num,int *arr);
void right_numerics(const int *dst,const int *src,int &nums,int &ords);
int main(int argc, char* argv[])
{
int i=0,t,a[4]={0},b[4]={0},m,n;
time_t tt;
ostream_iterator<int> oi(cout," ");
time(&tt);
srand(tt);
t=1000+random(9000);
num2array(t,a);
do{
cout<<"Enter a number in [1000,9999],0 to end:";
cin>>t;
if(t==0){
cout<<"This is ";
copy(&a[0],&a[wei],oi);
break;
}
num2array(t,b);
right_numerics(b,a,m,n);
cout<<"Right numerics:"<<m<<" and right ordering:"<<n<<endl;
i++;
if(n==wei){
cout<<"Very good! You guess "<<i<<" times."<<endl;
break;
}
}while(true);
return 0;
}
void num2array(int num,int *arr){
for (int i=wei-1;i>=0;i--){
arr[i]=num%10;
num/=10;
}
}
void right_numerics(const int *dst,const int *src,int &nums,int &ords){
vector <int> v(wei);
vector<int>::iterator it;
int i;
nums=ords=0;
for(i=0;i<wei;i++)v[i]=src[i];
for(i=0;i<wei;i++){
if(dst[i]==v[i]){
nums++;
ords++;
v[i]=-1;
}
else if(it=find(v.begin(),v.end(),dst[i]),it!=v.end()){
*it=-1;
nums++;
}
}
}