Re: [問題] 1-9位數不重複印出來 (Go)

作者: Neisseria (Neisseria)   2016-12-07 07:23:03
補上 Go (golang) 的版本
package main
import (
"bufio"
"errors"
"fmt"
"math"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Please input a number: ")
n, err := Input(scanner)
if err != nil {
os.Exit(1)
}
count := 1
for i := 1; i < int(math.Pow10(n)); i++ {
s := strconv.Itoa(i)
hash := make(map[rune]int)
for _, e := range s {
hash[e] += 1
}
digit := 0
j := i
for j > 0 {
j = j / 10
digit += 1
}
if len(hash) >= digit {
fmt.Printf("%d ", i)
count += 1
}
if count%10 == 0 {
fmt.Println("")
}
}
fmt.Println("")
}
func Input(scanner *bufio.Scanner) (int, error) {
if scanner.Scan() {
n, err := strconv.Atoi(scanner.Text())
if err != nil {
return 0, errors.New("Failed converting number")
}
return n, nil
} else {
return 0, errors.New("Failed reading from stdin")
}
}
※ 引述《mikemagic88 (Mikemagic88)》之銘言:
: 使用者輸入1 印1-9
: 使用者輸入2 印1-98 (11, 22, 33等重複的不印)
: 使用者輸入3 印1-987 (121, 988, 667等有重複的不印)

Links booklink

Contact Us: admin [ a t ] ucptt.com