技巧:
递归
思路:
1 把N-1块盘子从A移动到C F(n-1)
2 把最大的移动到B 1
3 把N-1块盘子从C移动到A F(n-1)
4 把最大的移动到C 1
5 把N-1块盘子从A移动到C F(n-1)
F(n) = 3*F(n-1) + 2
实现:
package main import ( "io" "os" "bufio" "strconv" . "fmt" ) func process(n int) int{ if n == 1 { return 2 }else { return 3 * process(n - 1) + 2 } } // https://ac.nowcoder.com/acm/problem/14585 func NC14585(_r io.Reader, _w io.Writer){ sc, out := bufio.NewScanner(_r), bufio.NewWriter(_w) defer out.Flush() for sc.Scan(){ n, _ := strconv.Atoi(sc.Text()) Fprintln(out, process(n)) } } func main(){ NC14585(os.Stdin, os.Stdout) }