Jump to content
IPS Community Suite 简体中文
Sign in to follow this  
ipscn

Q4

Recommended Posts

回文数是指从左边读和从右边读都一样的数。由最大的两个两位数的乘积构成的回文数是91×99=9009 。

求由两个最大的3位数的乘积组成的回文数。

package main

import (
	"fmt"
	"math"
)

func isPalindromicNumber(intV int64) bool {

	var remainder, temp int64
	var reverse int64 = 0
	temp = intV
	for {
		remainder = intV % 10
		reverse = int64(reverse*10 + remainder)
		intV /= 10

		if intV == 0 {
			break
		}
	}

	return temp == reverse
}

func GetMaxPalindromicNumber(lenV int64) []int64 {

	var minV int64 = int64(math.Pow(float64(10), float64(lenV-1)))
	var maxV int64 = int64(math.Pow(float64(10), float64(lenV))) - 1

	var x int64 = 0
	var y int64 = 0
	var product int64 = 0
	var max int64 = 0

	var r []int64
	r = make([]int64, 3)

	/*	fmt.Println(minV)
		fmt.Println(maxV)*/
	for x = minV; x <= maxV; x++ {
		for y = x; y <= maxV; y++ {
			product = x * y
			if isPalindromicNumber(product) {
				if max < product {
					max = product
					r[0] = x
					r[1] = y
					r[2] = max
				}
			}
		}

	}

	return r
}

func main() {

	var lenV int64 = 3

	fmt.Println(GetMaxPalindromicNumber(lenV))

}

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×