3793. 【NOIP2014模拟8.20】数字对 (Standard IO){类似欧几里得}

Description

对于一个数字对(a, b),我们可以通过一次操作将其变为新数字对(a+b, b)或(a, a+b)。
给定一正整数n,问最少需要多少次操作可将数字对(1, 1)变为一个数字对,该数字对至少有一个数字为n。

Input

第一行一个正整数 n

Output

一个整数表示答案。

Sample Input

5

Sample Output

3

Data Constraint

对于30%的数据, 1 <= n <= 1000
对于60%的数据, 1 <= n <= 20000
对于100%的数据,1 <= n <= 10^6

Hint

扫描二维码关注公众号,回复: 2128400 查看本文章

【样例解释】
(1,1)  →  (1,2)  →  (3,2)  →  (5,2)

。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int ans=1000000;
int cl(int a,int b)
 {
    if(b==1)return a-1;
    if(a==0||b==0||a==b)return 1e9;
    if(a<b)swap(a,b);
    return cl(a%b,b)+a/b;
 }
int main()
 {
    scanf("%d",&n);
    for(int i=1;i<n;i++)
     ans=min(ans,cl(n,i));

    printf("%d",ans); 
 }

猜你喜欢

转载自blog.csdn.net/ssl_trx/article/details/80992514
今日推荐