dpr77335 2017-09-22 07:17
浏览 26
已采纳

太多的结果导致欧拉计画#145的循环

I am trying to create a solution for Project Euler #145. I am writing in Go. When I run my program I get a result of 125. The expected result is 120. I have 2 different ways I have tried to write the code but both come up with the same answer. Any help pointing out my error would be appreciated.

Code option #1 using strings:

package main

import (
    "fmt"
    "strconv"
)

//checks to see if all the digits in the number are odd
func is_Odd(sum int) bool {
    intString := strconv.Itoa(sum)

    for x := len(intString); x > 0; x-- {
        newString := intString[x-1]
        if newString%2 == 0 {
            return false
        }
    }
    return true
}

//reverse the number passed
func reverse_int(value int) int {

    intString := strconv.Itoa(value)

    newString := ""

    for x := len(intString); x > 0; x-- {
        newString += string(intString[x-1])
    }

    newInt, err := strconv.Atoi(newString)

    if err != nil {
        fmt.Println("Error converting string to int")
    }

    return newInt
}

//adds 2 int's passed to it and returns an int
func add(x int, y int) int {
    return x + y
}

func main() {
    //functions test code
    /*y := 35
      x := reverse_int(y)
      z := add(x,y)
      fmt.Println(is_Odd(z))*/

    counter := 1

    for i := 1; i < 1000; i++ {
        flipped := reverse_int(i)
        sum := add(flipped, i)
        oddCheck := is_Odd(sum)
        if oddCheck {
            fmt.Println(counter, ":", i, "+", flipped, "=", sum)
            counter++
        }
    }
    counter--
    fmt.Println("total = ", counter)

}

Code option #2 using only ints:

package main

import (
    "fmt"
)

var counter int

//breaks down an int number by number and checks to see if
//all the numbers in the int are odd
func is_Odd(n int) bool {

    for n > 0 {
        remainder := n % 10
        if remainder%2 == 0 {
            return false
        }
        n /= 10
    }
    return true
}

//adds 2 int's passed to it and returns an int
func add(x int, y int) int {
    return x + y
}

//reverses the int passed to it and returns an int
func reverse_int(n int) int {
    var new_int int
    for n > 0 {
        remainder := n % 10
        new_int *= 10
        new_int += remainder
        n /= 10
    }
    return new_int
}

func main() {
    //functions test code
    /*y := 35
      x := reverse_int(y)
      z := add(x,y)
      fmt.Println(is_Odd(z))*/

    counter = 1

    for i := 1; i < 1000; i++ {
        flipped := reverse_int(i)
        sum := add(flipped, i)
        oddCheck := is_Odd(sum)
        if oddCheck {
            //fmt.Println(counter,":",i,"+",flipped,"=",sum)
            counter++
        }
    }
    counter--
    fmt.Println(counter)

}
  • 写回答

3条回答 默认 最新

  • duan02468 2017-09-22 08:47
    关注

    Leading zeroes are not allowed in either n or reverse(n) so in reverse(n int) int remove Leading zeroes like so:

    remainder := n % 10
    if first {
        if remainder == 0 {
            return 0
        }
        first = false
    }
    

    try this:

    package main
    
    import (
        "fmt"
    )
    
    //breaks down an int number by number and checks to see if
    //all the numbers in the int are odd
    func isOdd(n int) bool {
        if n <= 0 {
            return false
        }
        for n > 0 {
            remainder := n % 10
            if remainder%2 == 0 {
                return false
            }
            n /= 10
        }
        return true
    }
    
    //adds 2 int's passed to it and returns an int
    func add(x int, y int) int {
        return x + y
    }
    
    //reverses the int passed to it and returns an int
    func reverse(n int) int {
        first := true
        t := 0
        for n > 0 {
            remainder := n % 10
            if first {
                if remainder == 0 {
                    return 0
                }
                first = false
            }
            t *= 10
            t += remainder
            n /= 10
        }
        return t
    }
    
    func main() {
        counter := 0
        for i := 0; i < 1000; i++ {
            flipped := reverse(i)
            if flipped == 0 {
                continue
            }
            sum := add(flipped, i)
            if isOdd(sum) {
                counter++
                //fmt.Println(counter, ":", i, "+", flipped, "=", sum)
            }
        }
        fmt.Println(counter)
    }
    

    output:

    120
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题