Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: None  |  size: 10.32 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Programming Problem - Flipping coins
  2. import java.util.Scanner;
  3.  
  4. public class FLIPCOIN4 {
  5.     public static void main(String args[])
  6.     {
  7.         Scanner in = new Scanner(System.in);
  8.         //take N = no of coins, Q = no of commands as input
  9.         String s =  in.nextLine();
  10.         String[] str = s.split(" ");
  11.         int N = Integer.parseInt(str[0]);
  12.         int Q= Integer.parseInt(str[1]);
  13.         int[] output = new int[Q];
  14.         int[] input;
  15.         //input - array of integers (one integer for 32 coins)
  16.         if(N%32>0)
  17.             input = new int[N/32 + 1];
  18.         else
  19.             input = new int[N/32];
  20.         //initialize to 0 = tails
  21.         for(int i=0;i<input.length;i++)
  22.             {
  23.                 input[i] = 0;
  24.  
  25.             }
  26.         int out = 0;
  27.         int temp1 = 0;
  28.         int temp2 = 0;
  29.         int command = 0;
  30.         int RangeL = 0;
  31.         int RangeR = 0;
  32.         //looping over all Q commands
  33.         while(Q>0) {
  34.  
  35.             s = in.nextLine();
  36.             str = s.split(" ");
  37.             //command - command code
  38.             //RangeL,RangeR - range of coins which are affected
  39.             command = Integer.parseInt(str[0]);
  40.             RangeL = Integer.parseInt(str[1]);
  41.             RangeR = Integer.parseInt(str[2]);
  42.             // command==0 => if coins are to be flipped
  43.             if(command==0) {
  44.                 //if the coins range is over multiple numbers in array input[]
  45.                 if(RangeR/32>RangeL/32) {
  46.                     if(RangeL%32==0)
  47.                         input[RangeL/32] = input[RangeL/32] ^ -1;
  48.                     else if(RangeL%32==1)
  49.                         input[RangeL/32] =
  50.                             input[RangeL/32] ^ Integer.MAX_VALUE;
  51.                     else
  52.                         input[RangeL/32] =
  53.                             input[RangeL/32] ^ (int)Math.pow(2, 32-(RangeL%32));
  54.  
  55.                     if(RangeR%32==0)
  56.                         input[RangeR/32] = input[RangeL/8] ^ Integer.MIN_VALUE;
  57.                     else
  58.                         input[RangeR/32] =
  59.                             input[RangeR/8] ^
  60.                             (Integer.MIN_VALUE
  61.                              + ( Integer.MAX_VALUE +1-
  62.                                  (int) Math.pow(2, 31-(RangeL%32))));
  63.                     if(RangeR/32 - RangeL/32 > 1) {
  64.                         for(int i=RangeL/32+1; i <RangeR/32 ; i++) {
  65.                             input[i] = input[i] ^ -1;
  66.                         }
  67.                     }
  68.                 }//if the coins range is contained in one single integer in input[]
  69.                 else if(RangeR/32==RangeL/32) {
  70.                     if(RangeL%32==0 && RangeR%32==0)
  71.                         input[RangeL/32] = input[RangeL/32] ^
  72.                             Integer.MIN_VALUE;
  73.                     else if(RangeL%32==0 && RangeR%32==1)
  74.                         input[RangeL/32] = input[RangeL/32] ^
  75.                             (Integer.MIN_VALUE + (int) Math.pow(2, 30));
  76.                     else if(RangeL%32==0 && RangeR%32 >1)
  77.                         input[RangeL/32] = input[RangeL/32] ^
  78.                             (Integer.MIN_VALUE +  Integer.MAX_VALUE +1 -
  79.                              (int) Math.pow(2, 31-(RangeR%32)));
  80.                     else if(RangeL%32==1 && RangeR%32 ==1)
  81.                         input[RangeL/32] = input[RangeL/32] ^
  82.                             (int) Math.pow(2, 30);
  83.                     else if(RangeL%32==1 && RangeR%32 >1)
  84.                         input[RangeL/32] = input[RangeL/32] ^
  85.                             (Integer.MAX_VALUE +1 -
  86.                              (int) Math.pow(2, 31-(RangeR%32)));
  87.                     else
  88.                         input[RangeL/32] = input[RangeL/32] ^
  89.                             ((int) Math.pow(2, 32-(RangeL%32)) -
  90.                              (int) Math.pow(2, 31-(RangeR%32)) );
  91.  
  92.  
  93.                 }
  94.             }// command==1 => no of heads is to be reported
  95.             else if(command==1) {//if the coins range is contained in a single integer
  96.                 if(RangeR/32 == RangeL/32) {
  97.                     temp1 = input[RangeL/32]<< RangeL%32;
  98.                     temp1 = temp1 >>> RangeL%32;
  99.                     temp1 = temp1 >>> (31 - RangeR%32);
  100.                     temp1 = temp1 << (31 - RangeR%32);
  101.                     output[out] = Integer.bitCount(temp1);
  102.                 }
  103.                 //if the coins range is over multiple numbers in array input[]
  104.                 else if(RangeR/32>RangeL/32) {
  105.                     temp1 = input[RangeL/32]<< RangeL%32;
  106.                     temp1 = temp1 >>> RangeL%32;
  107.  
  108.                     temp2 = input[RangeL/32] >>> (31 - RangeR%32);
  109.  
  110.                     temp2 = temp2 << (31 - RangeR%32);
  111.                     output[out] =
  112.                         Integer.bitCount(temp1)+ Integer.bitCount(temp2);
  113.  
  114.                 }
  115.  
  116.                 if(RangeR/32 - RangeL/32 > 1) {
  117.                     for(int i=RangeL/32+1; i <RangeR/32 ; i++) {
  118.                         output[out] = output[out] + Integer.bitCount(input[i]);
  119.                     }
  120.                 }
  121.                 out++;
  122.             }
  123.             Q--;
  124.         }
  125.         for(int i =0;i<out;i++) {
  126.             System.out.println(output[i]);
  127.         }
  128.     }
  129. }
  130.        
  131. import java.util.Scanner;
  132.  
  133. public class FLIPCOIN4 {
  134.        
  135. public static void main(String args[])
  136.     {
  137.        
  138. Scanner in = new Scanner(System.in);
  139.         //take N = no of coins, Q = no of commands as input
  140.         String s =  in.nextLine();
  141.         String[] str = s.split(" ");
  142.         int N = Integer.parseInt(str[0]);
  143.         int Q= Integer.parseInt(str[1]);
  144.        
  145. int[] output = new int[Q];
  146.        
  147. int[] input;
  148.         //input - array of integers (one integer for 32 coins)
  149.         if(N%32>0)
  150.             input = new int[N/32 + 1];
  151.         else
  152.             input = new int[N/32];
  153.        
  154. //initialize to 0 = tails
  155.         for(int i=0;i<input.length;i++)
  156.             {
  157.                 input[i] = 0;
  158.             }
  159.        
  160. int out = 0;
  161.         int temp1 = 0;
  162.         int temp2 = 0;
  163.         int command = 0;
  164.         int RangeL = 0;
  165.         int RangeR = 0;
  166.        
  167. //looping over all Q commands
  168.         while(Q>0) {
  169.  
  170.             s = in.nextLine();
  171.             str = s.split(" ");
  172.             //command - command code
  173.             //RangeL,RangeR - range of coins which are affected
  174.             command = Integer.parseInt(str[0]);
  175.             RangeL = Integer.parseInt(str[1]);
  176.             RangeR = Integer.parseInt(str[2]);
  177.        
  178. // command==0 => if coins are to be flipped
  179.        
  180. if(command==0) {
  181.                 //if the coins range is over multiple numbers in array input[]
  182.                 if(RangeR/32>RangeL/32) {
  183.                     if(RangeL%32==0)
  184.        
  185. input[RangeL/32] = input[RangeL/32] ^ -1;
  186.                     else if(RangeL%32==1)
  187.                         input[RangeL/32] =
  188.                             input[RangeL/32] ^ Integer.MAX_VALUE;
  189.                     else
  190.                         input[RangeL/32] =
  191.                             input[RangeL/32] ^ (int)Math.pow(2, 32-(RangeL%32));
  192.        
  193. if(RangeR%32==0)
  194.                         input[RangeR/32] = input[RangeL/8] ^ Integer.MIN_VALUE;
  195.                     else
  196.                         input[RangeR/32] =
  197.                             input[RangeR/8] ^
  198.                             (Integer.MIN_VALUE
  199.                              + ( Integer.MAX_VALUE +1-
  200.                                  (int) Math.pow(2, 31-(RangeL%32))));
  201.        
  202. if(RangeR/32 - RangeL/32 > 1) {
  203.                         for(int i=RangeL/32+1; i <RangeR/32 ; i++) {
  204.                             input[i] = input[i] ^ -1;
  205.                         }
  206.                     }
  207.        
  208. }//if the coins range is contained in one single integer in input[]
  209.                 else if(RangeR/32==RangeL/32) {
  210.        
  211. if(RangeL%32==0 && RangeR%32==0)
  212.                         input[RangeL/32] = input[RangeL/32] ^
  213.                             Integer.MIN_VALUE;
  214.                     else if(RangeL%32==0 && RangeR%32==1)
  215.                         input[RangeL/32] = input[RangeL/32] ^
  216.                             (Integer.MIN_VALUE + (int) Math.pow(2, 30));
  217.                     else if(RangeL%32==0 && RangeR%32 >1)
  218.                         input[RangeL/32] = input[RangeL/32] ^
  219.                             (Integer.MIN_VALUE +  Integer.MAX_VALUE +1 -
  220.                              (int) Math.pow(2, 31-(RangeR%32)));
  221.                     else if(RangeL%32==1 && RangeR%32 ==1)
  222.                         input[RangeL/32] = input[RangeL/32] ^
  223.                             (int) Math.pow(2, 30);
  224.                     else if(RangeL%32==1 && RangeR%32 >1)
  225.                         input[RangeL/32] = input[RangeL/32] ^
  226.                             (Integer.MAX_VALUE +1 -
  227.                              (int) Math.pow(2, 31-(RangeR%32)));
  228.                     else
  229.                         input[RangeL/32] = input[RangeL/32] ^
  230.                             ((int) Math.pow(2, 32-(RangeL%32)) -
  231.                              (int) Math.pow(2, 31-(RangeR%32)) );
  232.        
  233. }
  234.             }// command==1 => no of heads is to be reported
  235.             else if(command==1) {//if the coins range is contained in a single integer
  236.                 if(RangeR/32 == RangeL/32) {
  237.                     temp1 = input[RangeL/32]<< RangeL%32;
  238.                     temp1 = temp1 >>> RangeL%32;
  239.                     temp1 = temp1 >>> (31 - RangeR%32);
  240.                     temp1 = temp1 << (31 - RangeR%32);
  241.        
  242. output[out] = Integer.bitCount(temp1);
  243.                 }
  244.                 //if the coins range is over multiple numbers in array input[]
  245.                 else if(RangeR/32>RangeL/32) {
  246.                     temp1 = input[RangeL/32]<< RangeL%32;
  247.                     temp1 = temp1 >>> RangeL%32;
  248.  
  249.                     temp2 = input[RangeL/32] >>> (31 - RangeR%32);
  250.  
  251.                     temp2 = temp2 << (31 - RangeR%32);
  252.                     output[out] =
  253.                         Integer.bitCount(temp1)+ Integer.bitCount(temp2);
  254.                 }
  255.  
  256.                 if(RangeR/32 - RangeL/32 > 1) {
  257.                     for(int i=RangeL/32+1; i <RangeR/32 ; i++) {
  258.                         output[out] = output[out] + Integer.bitCount(input[i]);
  259.                     }
  260.                 }
  261.        
  262. out++;
  263.        
  264. }
  265.             Q--;
  266.         }
  267.         for(int i =0;i<out;i++) {
  268.             System.out.println(output[i]);
  269.         }
  270.        
  271. }
  272. }
  273.        
  274. int[] output = new int[Q];
  275.     int[] input;
  276.     //input - array of integers (one integer for 32 coins)
  277.     if(N%32>0)
  278.         input = new int[N/32 + 1];
  279.     else
  280.         input = new int[N/32];
  281.        
  282. s = in.nextLine();
  283.         str = s.split(" ");