SuitNdtie

Mastermind EXAM05

Mar 27th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<stdio.h>
  2. void inttochar(char now[],int I){
  3.     for(int i=0;i<=3;i++)now[i] = '0';now[4] = '\0';
  4.         int ti = I;int nin = 3;
  5.         while(ti!=0){
  6.             now[nin--] = (ti%10)+'0';
  7.             ti/=10;
  8.     }
  9. }
  10.  
  11. int main()
  12. {
  13.     int m;
  14.     scanf("%d",&m);
  15.     char tries[m][5];
  16.     int PosC[m];
  17.     int NumC[m];
  18.     for(int i=0;i<m;i++){
  19.         scanf("%s",tries[i]);
  20.         scanf("%d %d",&PosC[i],&NumC[i]);
  21.     }
  22.     for(int i=0;i<=9999;i++){
  23.         char now[5];
  24.         inttochar(now,i);
  25.         bool possible = true;
  26.         for(int j=0;j<m && possible;j++){
  27.             int countpos = 0;
  28.             for(int k=0;k<4;k++){
  29.                 if(now[k] == tries[j][k]){
  30.                     countpos++;
  31.                 }
  32.             }
  33.             if(countpos != PosC[j])possible = false;
  34.            
  35.             if(possible){
  36.                 int checkNum[10];for(int i=0;i<10;i++)checkNum[i] = 0;
  37.                 for(int k=0;k<4;k++){
  38.                     checkNum[tries[j][k] - '0']++;
  39.                 }
  40.                 int countNum = 0;
  41.                 for(int k=0;k<4;k++){
  42.                     if(checkNum[now[k] - '0'] > 0){
  43.                         countNum++;
  44.                         checkNum[now[k] - '0']--;
  45.                     }
  46.                 }
  47.                 if(countNum-countpos != NumC[j])possible = false;  
  48.             }
  49.            
  50.         }
  51.        
  52.         if(possible)
  53.             printf("%s\n",now);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment