求教!16分
查看原帖
求教!16分
1001552
newsname楼主2023/7/22 19:21
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
using namespace std;
int lcm( int x, int y)
{
return x/__gcd(x,y)*y;
}
std::string check_product(int n, std::vector<int>& a) {
    // 计算最大公约数和最小公倍数
    int g = __gcd(a[0], a[1]);
    int l = lcm(a[0], a[1]);
    for (int i = 2; i < n; i++) {
        g = __gcd(g, a[i]);
        l = lcm(l, a[i]);
    }
    
    // 判断 l * g 是否等于 a_1 * a_2 * ... * a_n
    if (l * g == std::accumulate(a.begin(), a.end(), 1, std::multiplies<int>())) {
        return "Yes";
    } else {
        return "No";
    }
}

int main() {
    // 读取测试数据组数
    int T;
    std::cin >> T;

    // 处理每组数据
    for (int t = 0; t < T; t++) {
        // 读取整数个数和整数列表
        int n;
        std::cin >> n;
        std::vector<int> a(n);
        for (int i = 0; i < n; i++) {
            std::cin >> a[i];
        }
        
        // 调用函数进行判断,并输出结果
        std::string result = check_product(n, a);
        std::cout << result << std::endl;
    }

    return 0;
}

这思路有啥问题吗?

2023/7/22 19:21
加载中...