Spocoman

10. Odd Even Sum

Dec 23rd, 2021 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. odd_sum = 0
  2. even_sum = 0
  3. num = int(input())
  4.  
  5. for i in range(1, num + 1):
  6.     if i % 2 == 1:
  7.         odd_sum += int(input())
  8.     else:
  9.         even_sum += int(input())
  10.  
  11. if odd_sum == even_sum:
  12.     print('Yes')
  13.     print(f'Sum = {odd_sum}')
  14. else:
  15.     print('No')
  16.     print(f'Diff = {abs(even_sum - odd_sum)}')
  17.  
  18.  
  19. Решение с тернарен оператор:
  20.  
  21. odd_sum = 0
  22. even_sum = 0
  23. num = int(input())
  24.  
  25. for i in range(1, num + 1):
  26.     odd_sum += int(input()) if i % 2 == 1 else 0
  27.     even_sum += int(input()) if i % 2 == 0 else 0
  28.  
  29. print(f'Yes\nSum = {odd_sum}' if odd_sum == even_sum else f'No\nDiff = {abs(even_sum - odd_sum)}')
  30.  
Add Comment
Please, Sign In to add comment