Advertisement
Aminpro

[ECP1026] In Lab 2, Question 4

Dec 15th, 2012
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. //https://www.facebook.com/AminproPastebin
  2. // Credits to Wei Wen through Joshua Soo for solving the distinct check using bitmap.
  3. #include <stdio.h>
  4. #define LIMIT 10
  5.  
  6. typedef unsigned int set;
  7.  
  8. int main(){
  9.  
  10.         set master;
  11.         set guess;
  12.         int a, b, c, d;
  13.         master = (1<<1)|(1<<0)|(1<<2)|(1<<6); //
  14.        
  15.         while(1){
  16.                 int i;
  17.                 int hits =0;
  18.                 int flag = 0;
  19.                
  20.                 guess = (0<<0)|(0<<1)|(0<<2)|(0<<3)|(0<<4)|(0<<5)|(0<<6)|(0<<7)|(0<<8)|(0<<9);
  21.                
  22.                 printf("Guess: ");
  23.                 scanf("%d%d%d%d", &a, &b, &c, &d );
  24.                 guess = (1<<a)|(1<<b)|(1<<c)|(1<<d);
  25.                
  26.                 for(i=0;i<LIMIT;i++){
  27.                 if((1<<i)&guess){                      
  28.             flag++;
  29.             }
  30.         }
  31.  
  32.                 if(flag >= 4){
  33.                         for(i=0; i<LIMIT; i++){
  34.                                 if ((1<<i) &guess &master){
  35.                
  36.                                 hits++;
  37.                         }
  38.                 }      
  39.                 printf("%d hit(s)\n", hits);
  40.                 }
  41.                 else
  42.                         printf("Your guess must contain 4 distinct numbers\n");
  43.         }
  44.         return 0;      
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement