dotcpp0715281


私信TA

用户名:dotcpp0715281

访问量:106

签 名:

等  级
排  名 23387
经  验 590
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 河南大学
专  业

  自我简介:

解题思路:
分奇偶两种情况考虑。

注意事项:

参考代码:

#include <stdio.h>
#include <iostream>
 
using namespace std;
 
int gcd(int a, int b){
    int t;
    for(int i = 1; i <= a && i <= b; i++){
        if(a%i == 0 && b % i == 0){
            t = i;
        }
    }
    return t;
}
 
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        int l;
        scanf("%d", &l);
        if(l % 2 == 0){
            printf("4\n");
        }else{
            int max = 1;
            int s1;
            for(int j = 1; j <= l/2; j++){
                if(max <= gcd(j, l-j)){
                    s1 = j;
                    max = gcd(j, l-j);
                }
            }
            int res = 2;
            int x = l-s1, y = s1;
            while(x > 0 && y > 0){
                if(x > y){
                    res += 2 * (x/y);
                    x %= y;
                }else{
                    res += 2 * (y/x);
                    y %= x;
                }
            }
            printf("%d\n", res);
            return 0;
        }
    }
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区