当前位置:网站首页>C language - Blue Bridge Cup - promised score

C language - Blue Bridge Cup - promised score

2022-07-06 02:38:00 @Xiaoyu~

Title Description
If the greatest common divisor of the numerator and denominator of a fraction is 1, This fraction is called a reduced fraction .
for example ,3/4 , 5/2 , 1/8 , 7/1 It's all contract scores .
Excuse me, , How many committed scores are there , The numerator and denominator are 1 To 2020 Integer between ( Include 1 and 2020)?
Topic analysis
Violent search , Judge by the greatest common divisor

1. Import header file first

aa8dc2959e924a18b8da4d81bd0142b4.png

2. Then design a function to find the greatest common factor of molecules , if 1, Then the score is a reduced score

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQOWwj-Wuh34=,size_20,color_FFFFFF,t_70,g_se,x_16

3. The main function with for loop , Bring each number into the numerator and denominator respectively , then num Count

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQOWwj-Wuh34=,size_20,color_FFFFFF,t_70,g_se,x_16

 4. The final result is 2481215

e9e3f622e5894089b88d3dc93c4c9d2f.png

 5. The attached total code is as follows

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
// Design masure Function to find the greatest common factor between numerator and denominator 
int masure(int i, int n)
{
	if (n % i == 0)
	{
		return i;
	}
	else if (n % i != 0)
	{
		return masure(n%i,i);
	}
}
int main()
{
	int num = 0;
	int i = 0;
	int n = 0;
	// use for Loop so that each number is made a numerator and denominator respectively 
	for (i = 1; i < 2021; i++)
	{
		for (n = 1; n < 2021; n++)
		{
			int x = masure(i,n);
			// If the greatest common factor is 1, Then the score is a reduced score 
			if (x==1)
			{
				//num Number of Statistics 
				num++;
			}
		}
	}
	printf("%d\n", num);
	return 0;
}

原网站

版权声明
本文为[@Xiaoyu~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140009206281.html