Advertisement
Spocoman

04. Sum of Two Numbers

Sep 24th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.27 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6.    var startNum, endNum, magicNum, counter int
  7.     fmt.Scanln(&startNum)
  8.     fmt.Scanln(&endNum)
  9.     fmt.Scanln(&magicNum)
  10.    
  11.     var isMagic = false
  12.  
  13.     for i := startNum; i <= endNum; i++ {
  14.         for j := startNum; j <= endNum; j++ {
  15.             counter++
  16.             if i + j == magicNum {
  17.                 fmt.Printf("Combination N:%d (%d + %d = %d)\n", counter, i, j, magicNum)
  18.                 isMagic = true
  19.                 break
  20.             }
  21.         }
  22.         if isMagic == true {
  23.             break
  24.         }
  25.     }
  26.        
  27.     if isMagic == false {
  28.         fmt.Printf("%d combinations - neither equals %d\n", counter, magicNum)
  29.     }
  30. }
  31.  
  32. ИЛИ:
  33.  
  34. package main
  35.  
  36. import "fmt"
  37.  
  38. func main() {
  39.    var startNum, endNum, magicNum, counter int
  40.     fmt.Scanln(&startNum)
  41.     fmt.Scanln(&endNum)
  42.     fmt.Scanln(&magicNum)
  43.    
  44.     for i := startNum; i <= endNum; i++ {
  45.         for j := startNum; j <= endNum; j++ {
  46.             counter++
  47.             if i + j == magicNum {
  48.                 fmt.Printf("Combination N:%d (%d + %d = %d)\n", counter, i, j, magicNum)
  49.                 return
  50.             }
  51.         }
  52.     }
  53.        
  54.     fmt.Printf("%d combinations - neither equals %d\n", counter, magicNum)
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement