标签:style blog color java for ar div log
AC CODE:
这里有个要注意的地方就是“数对中一个数是另一个数的两倍。”这一句话。
看到了这一句话,可能你就会想到用(n1 / n2 == 2 || n2 / n1 == 2)这样的判断语句来做判断,如果真是这样,那 5 / 2怎么办?
所以,这里我们要做一个小转变,那就是:(n1 * 2 == n2 || n2 * 2 == n1)
import java.util.Scanner; public class Main { private static int[] getNumbers(String[] strings) { int[] numbers = new int[strings.length]; for (int i = 0; i < strings.length; i++) { numbers[i] = Integer.parseInt(strings[i]); } return numbers; } private static boolean isDouble(int n1, int n2) { if (n1 == 0 || n2 == 0) { return false; } if ((n1 * 2 == n2) || (n2 * 2 == n1)) { return true; } return false; } private static int getDoubleCounts(int[] ns) { int count = 0; for (int i = 0; i < ns.length; i++) { for (int j = i + 1; j < ns.length; j++) { if (isDouble(ns[i], ns[j])) { count++; } } } return count; } public static void main(String[] args) { Scanner input = new Scanner(System.in); while (true) { String s = input.nextLine(); if (s.equals("-1")) { break; } String[] numStrings = s.split(" "); int[] number = getNumbers(numStrings); System.out.println(getDoubleCounts(number)); } } }
标签:style blog color java for ar div log
原文地址:http://www.cnblogs.com/Stone-sqrt3/p/3925421.html