奇数和偶数位数之和的差值。
问题陈述
给定一个长整数 n,编写一个程序来查找奇数位和偶数位的差是 0 还是非 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
广告