package bitsgoroku; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Bitsugoroku { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); try { int result = 1; List resultList = new ArrayList(); resultList.add(result); int binCount = changeBinary(result); int dameCount = 0; while ((result + binCount) != n) { if ((result + binCount) > n) {// nを超えてしまう時 dameCount++; if (dameCount < 2) { binCount = changeBinary(result); result -= binCount; resultList.add(result); }else { break; } } else if ((result + binCount) < n) {// nを超えない時 binCount = changeBinary(result); result += binCount; resultList.add(result); } } System.out.println(resultList.size()); } catch (Exception e) { System.out.println(e); } } private static int changeBinary(int result) { String bin = Integer.toBinaryString(result); String[] binArray = bin.split(""); // 2進数の1の数を数える int binCount = 0; for (String sss : binArray) { if (sss.equals("1")) { binCount++; } } return binCount; } }