📌  相关文章
📜  Python程序用于奇数和偶数之和之间的差异

📅  最后修改于: 2022-05-13 01:56:56.531000             🧑  作者: Mango

Python程序用于奇数和偶数之和之间的差异

给定一个长整数,我们需要找出奇数和偶数和之间的差是否为0。索引从零开始(0 索引是最左边的数字)。

例子:

Input : 1212112
Output : Yes
Explanation:-
the odd position element is 2+2+1=5
the even position element is 1+1+1+2=5
the difference is 5-5=0.so print yes.

Input :12345
Output : No
Explanation:-
the odd position element is 1+3+5=9
the even position element is 2+4=6
the difference is 9-6=3 not  equal
to zero. So print no.

方法一:一一遍历数字,求两个和。如果两个和之间的差为 0,则打印 yes,否则打印 no。

方法2:这可以很容易地用11的整除率来解决。这个条件只有当数字能被11整除时才满足。所以检查数字是否能被11整除。

# Python program to check if difference between sum of
# odd digits and sum of even digits is 0 or not
  
def isDiff(n):
    return (n % 11 == 0)
  
# Driver code
n = 1243;
if (isDiff(n)):
    print("Yes")
else:
    print("No")
  
# Mohit Gupta_OMG <0_o>

输出:

Yes

请参阅完整的文章奇偶数和的差异了解更多详细信息!