# 42. Pairs of Numbers

https://blog.csdn.net/qq_43521140/article/details/107853492

- 出题人:OJ
- 标签:["DFS and Similar"]
- 难度:简单
- 总分数:100

## 题目描述
<p>Let's assume that we have a pair of numbers (a,b). We can get a new pair (a+b,b) or (a,a+b) from the given pair in a single step.</p><p>Let the initial pair of numbers be (1,1). Your task is to find number k, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n.</p>

## 解答要求
时间限制:1000ms, 内存限制:100MB

## 输入
<p>The input contains the only integer n (1 ≤ n ≤ 10<sup>6</sup>).<b>Process to the end of file</b>.</p>

## 输出
<p>Print the only integer k.</p>

## 样例
### 输入样例 1:
```
5
1
```
### 输出样例 1:
```
3
0

```
## 提示

```
package main
import "fmt"
var tmp int
var leastep int
func main() {
var n int
for {
_, err := fmt.Scanf("%d", &n)
if err != nil {
return
} else if n == 1 {
fmt.Printf("0\n")
continue
} else if n == 2 {
fmt.Printf("1\n")
continue
}
leastep = n - 1
for i:= 1;i<n;i++{
tmp = 0
dfs(n,i)
leastep = minint(tmp,leastep)
}
//leastep = n + 1
//for i := n/2 + 1; i < n && i/(n-i) <= leastep; i++ {
// leastep = minint(leastep, leaststeps(i, n-i, 1))
//}
fmt.Printf("%d\n", leastep)
}
}
func minint(a, b int) int {
if a >= b {
return b
} else {
return a
}
}
func dfs(a,b int){
if b == 1{
tmp += a-1
return
}
tmp += a/b
dfs(b,a%b)
}

func leaststeps(a, b, steps int) int {
if b == 1 {
return a - 1 + steps
} else if a%b == 0 {
return leastep + 1
} else if steps >= leastep {
return leastep + 1
} else {
return leaststeps(b, a%b, steps+a/b)
}
}

```

最新文章

  1. linux 安装mysql数据库——yum安装法
  2. ubuntu和centos安装RRDTool——cacti前置技能
  3. java 4种方式读取配置文件 + 修改配置文件
  4. 409. Longest Palindrome
  5. 用c#开发微信 (13) 微统计 - 阅读分享统计系统 3 UI设计及后台处理
  6. C#设计模式——组合模式(Composite Pattern)
  7. asp.net MVC 常见安全问题及解决方案
  8. Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)
  9. iOS开发之录音
  10. rsyslog 读取单个文件测试
  11. performance
  12. 基于visual Studio2013解决C语言竞赛题之1070删除相同节点
  13. Android 消息传递之Bundle的使用——实现object对象传输(二)
  14. 【Maven】项目中没有resources目录
  15. Android-Gradle(五)
  16. Qt学习--信号与槽(多窗口的实现)
  17. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet
  18. Sample Classification Code of CIFAR-10 in Torch
  19. encode()、decode()字符编码问题
  20. 说一下PHP中die()和exit()区别

热门文章

  1. 在 C# CLR 中学习 C++ 之了解 extern
  2. 01_Linux基础-部署-VMware-Xshell-Xftp-内核-安迪比尔定理
  3. 数据库基础操作 part1
  4. KingbaseES 中 JSON 介绍
  5. 如何自动清理 KingbaseES SYS_LOG
  6. Netty 学习(一):服务端启动 &amp; 客户端启动
  7. ESX添加过时的硬件
  8. Windows 10 索引设置
  9. Lua脚本在Redis事务中的应用实践
  10. 转载---Beats:如何使用Filebeat将MySQL日志发送到Elasticsearch