对于给定的长整数n,编写一个程序以查找奇数位和偶数位总和之间的差是否为0。索引从0开始。
n = 1212112 Sum of odd position elements = 2 + 2 + 1 = 5 Sum of even position elements = 1 + 1 + 1 + 2 = 5 Difference = 5 - 5 = 0 Output = Yes
以下是Java中的程序,用于查找所需的输出。
class JavaTester { public static int difference(int n){ return (n % 11); } public static void main(String args[]){ int n = 1212112; System.out.println("Number: " + n); System.out.println(difference(n) == 0 ? "Yes" : "No"); n = 12121121; System.out.println("Number: " + n); System.out.println(difference(n) == 0 ? "Yes" : "No"); } }
输出结果
Number : 1212112 Output: Yes Number : 12121121 Output: No