计算机挑战赛5

有N个正整数,求这N个正整数两两之间的最大公约数之积 输入: 第一行输入正整数N(N<=100)第二行有N个正整数(<10000)输出: 输出这N个正整数两两之间的最大公约数之积,结果对1000000007取模样例

输入:

4

6 8 9 10

样例输出:

24

代码:
C++:

面向对象:

#include<iostream>
#include<algorithm>
using namespace std;
class Soulution {
public:
	int gcd(int a, int b) {
		while (b != 0) {
			int temp = b;
			b = a % b;
			a = temp;
		}
		return a;
	}
};
bool cmp(int A, int B) {
	return A < B;
}
int main() {
	int n;
	cin >> n;
	Soulution s = Soulution();
	int res = 1;
	int arr[100];
	for (int i = 0; i < n; i++) {
		int num; cin >> num;
		arr[i] = num;
	}
	sort(arr, arr + n, cmp);
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			res *= s.gcd(arr[i], arr[j]);
		}
	}
	cout << res << endl;
	return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a, int b) {
	while (b != 0) {
		int temp = b;
		b = a % b;
		a = temp;
	}
	return a;
}
int cmp(int A, int B) {
	return A < B;
}
int main() {
	int n;
	cin >> n;
	int res = 1;
	int arr[100];
	for (int i = 0; i < n; i++) {
		int num; cin >> num;
		arr[i] = num;
	}
	sort(arr, arr + n, cmp);
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			res *= gcd(arr[i], arr[j]);
		}
	}
	cout << res << endl;
	return 0;
}

Python:

class Solution:
    def gcd(self, a, b):
        while b != 0:
            temp = b
            b = a % b
            a = temp
        return a
    def main(self):
        n = int(input())
        arr = []
        num = list(input().split())
        res = 1
        for i in range(n):
            arr.append(int(num[i]))
        arr = sorted(arr)
        for i in range(0, n - 1, 1):
            for j in range(i + 1, n, 1):
                res *= self.gcd(arr[i], arr[j])
        print(res)
if __name__ == "__main__":
    s = Solution()
    s.main()

Java:

package com.my.gududu;

import java.util.*;


class Solution{
    int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}
public class Main {
    public static void main(String[] args) {
        Solution s =new Solution();
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int arr[] = new int[100];
        for (int i = 0; i < n; i++) {
            int num;
            num = input.nextInt();
            arr[i] = num;
        }
        Arrays.sort(arr, 0, n);
        int res = 1;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res *= s.gcd(arr[i], arr[j]);
            }
        }
        System.out.println(res);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值