题解 | #最大公约数1#

最大公约数1

https://www.nowcoder.com/practice/021010dda9f04900a86738931a5600a4

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cmath>
using namespace std;

int arr[100000];  //数组开大点,不然会栈溢出或者越界

int gcd(int a, int b) {
    if (a == 0) {
        return b;
    }
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}

int main() {
    int n;
    while (scanf("%d", &n) != EOF) { // 注意 while 处理多个 case
        int maxnum, minnum; //最大值,最小值
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                scanf("%d", &arr[i]);
                maxnum = arr[i];
                minnum = arr[i];
            } else {
                scanf("%d", &arr[i]);
                maxnum = maxnum > arr[i] ? maxnum : arr[i];
                minnum = minnum < arr[i] ? minnum : arr[i];
            }
        }
        int t = gcd(maxnum, minnum);

        printf("%d %d %d\n", minnum, maxnum, t);

    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

MingoTree:看不出你你的技术栈,想找什么工作,然后课设项目别写上去了,自我评价删了,前后端你想好你要干啥,这种简历投上去秒挂的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
02-12 10:05
小米集团 算法工程师 28.0k*15.0
泡沫灬一触即破:楼上那个看来是看人拿高薪,自己又不如意搁这泄愤呢是吧,看你过往评论很难不怀疑你的精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务