2020第11届蓝桥杯C++B组(不确保答案正确性,仅供参考)蓝桥2020试题 B: 既约分数

【问题描述】

如果一个分数的分子和分母的最大公约数是 1,这个分数称为既约分数。
例如,34 , 52 , 18 , 71 都是既约分数。
请问,有多少个既约分数,分子和分母都是 1 到 2020 之间的整数(包括 1 和 2020)?

【答案提交】

这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

ans:2481215

不保证一定正确

#include<iostream>

using namespace std;

int gcd(int a, int b) {
    
    
	if (b==0)
		return a;
	return gcd(b, a%b);
}
int main() {
    
    
	int num = 0;
	const int N = 2020;
	for (int i = 1; i <= N; i++) {
    
    
		for (int j = 1; j <= N; j++) {
    
    
			if (gcd(i, j) == 1) {
    
    
				num++;
//				if (j == 2020)
//				cout << i << '/' << j << '\n';
			}
		}
	}
	cout << num;

	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_44378358/article/details/109132815