Advertisement
Spocoman

07. Left and Right Sum

Sep 20th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.07 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6. )
  7.  
  8. func main() {
  9.     var n, number int
  10.     fmt.Scanln(&n)
  11.    
  12.     var leftSum = 0
  13.     var rightSum = 0
  14.  
  15.     for i := 0; i < n; i++ {
  16.         fmt.Scanln(&number)
  17.         leftSum += number
  18.     }
  19.  
  20.     for i := 0; i < n; i++ {
  21.         fmt.Scanln(&number)
  22.         rightSum += number
  23.     }
  24.  
  25.     if (leftSum == rightSum) {
  26.         fmt.Printf("Yes, sum = %d\n", leftSum)
  27.     } else {
  28.         fmt.Printf("No, diff = %d\n", int(math.Abs(float64(leftSum - rightSum))))
  29.     }
  30. }
  31.  
  32. ИЛИ:
  33.  
  34. package main
  35.  
  36. import (
  37.     "fmt"
  38.     "math"
  39. )
  40.  
  41. func main() {
  42.     var n, number int
  43.     fmt.Scanln(&n)
  44.    
  45.     var leftSum = 0
  46.     var rightSum = 0
  47.    
  48.     for i := -n; i < n; i++ {
  49.         fmt.Scanln(&number)
  50.         if (i < 0) {
  51.             leftSum += number
  52.         } else {
  53.             rightSum += number
  54.         }
  55.     }
  56.    
  57.     if (leftSum == rightSum) {
  58.         fmt.Printf("Yes, sum = %d\n", leftSum)
  59.     } else {
  60.         fmt.Printf("No, diff = %d\n", int(math.Abs(float64(leftSum - rightSum))))
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement