题解 | #最大公约数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")

全部评论

相关推荐

斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务