最简真分数(最小公倍数)

题目描述

给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合。

输入描述:

每组包含n(n<=600)和n个不同的整数,整数大于1且小于等于1000。

输出描述:

每行输出最简真分数组合的个数。

示例1

输入

7
3 5 7 9 11 13 15
3 
2 4 5
0

输出

17 
2
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <stack>
using namespace std;


//GreatestCommonDivisor
int gcd(int x, int y) {
	//y要求x大 y小
	if(x < y) {
		swap(x, y);
	}

	if (x % y == 0) {
		return y;
	} else {
		int yu = x % y;
		return gcd(y, yu);
	}

}


vector<int> res;
int n;
int t;
int main() {
	while(cin >> n && n != 0) {
		res.clear();
		int cnt = 0;
		for (int i = 1; i <= n; i++) {
			cin >> t;
			res.push_back(t);
		}

		for (int i = 0; i <= res.size() - 1; i++) {
			for (int j = i + 1; j <= res.size() - 1; j++) {
				int d = gcd(res[i],res[j]);
				if(d == 1) {
					cnt++;
				}

			}
		}

		cout << cnt << endl;
	}


}
发布了86 篇原创文章 · 获赞 0 · 访问量 3640

猜你喜欢

转载自blog.csdn.net/bijingrui/article/details/104684733