andy6804tw
7/15/2016 - 11:08 AM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1872 困難度 ★ 這題利用Java內建函示庫 Integer.toBinaryStr

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1872 困難度 ★ 這題利用Java內建函示庫 Integer.toBinaryString()以及Integer.bitCount()

import java.util.*;
 
public class Main {
 
 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   int n = scn.nextInt();
   if (n == 0)
    break;
   System.out.printf("The parity of %s is %d (mod 2).\n",Integer.toBinaryString(n), Integer.bitCount(n));
  }
 }
}