“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!

10. Common divisor

Method 1: Violent solution

Greatest common divisor: may be the smallest of the two,

So let’s divide by the smallest of the two numbers and keep subtracting by 1

/ / method
int main(a)
{
	int m = 0;
	int n = 0;
	int min = 0;
	scanf("%d %d", &m, &n);
	min = m < n ? m : n;	// Find the smallest of the two numbers
	while (min)
	{
		if (m % min == 0 && n % min == 0)
		{
			printf("The greatest common divisor of %d and %d is :%d\n",m, n, min);
			break;
		}
		min--;
	}
	return 0;
}
Copy the code

Method 2: Toss and turn division

int main(a)
{
	int m = 0;
	int n = 0;
	scanf("%d %d", &m,&n);
	int r = 0;
    // Toss and turn division
	while (r = m %n)
	{
		m = n;	// Assign n to m
		n = r;	// Assign the remainder to n
	}
	printf("%d\n", n);
	return 0;
}
Copy the code

11. Find the least Common multiple

Method 1: Force solution

Smallest common multiple: Probably the largest of the two

I just have to go +1 down

int main(a) 
{
	int n = 0;
	int m = 0;
	scanf("%d %d", &n, &m);
	int max = n > m ? n : m;	// The largest of the two
	while (1)
	{
		if (max % n == 0 && max % m == 0)
		{
			printf("%d %d least common multiple: %d\n", n, m, max);
			break;
		}
		max++;
	}
	return 0;
}
Copy the code

Method 2: least common multiple = product of two numbers/greatest common divisor

// Find the greatest common divisor
int MaxCommonDivisor(int m,int  n)
{
	int r = 0;
	// Toss and turn division
	while (r = m % n)
	{
		m = n;	// Assign n to m
		n = r;	// Assign the remainder to n
	}
	return n;
}
int main(a)
{
	int m = 0;
	int n = 0;
	scanf("%d %d", &m, &n);
	int ret = MaxCommonDivisor(m, n);
	printf("The least common multiple of %d %d is :%d, and the greatest common divisor is :%d \n", m, n, m * n / ret,ret);
	return 0;
}
Copy the code

12. 答 案 : Identify leap years between 1000 and 2000

Leap year:1.Can it be4Divisible, cannot be divided by100Aliquot &&2.Can it be400Aliquot | |Copy the code
int main(a)
{
	int year = 0;
	int count = 0;	/ / count
	for (year = 1000; year <= 2000; year++)
	{
		if (year % 4= =0 && year % 100! =0 || year % 400= =0)
		{
			count++;
			printf("%d ", year); }}printf("\n1000 to 2000, the number of leap years is :%d\n",count);
	return 0;
}
//count:243
Copy the code

// Can also be written as
int main(a)
{
    int count = 0;
    int year = 0;
    for(year = 1000; year <=2000; year++) {if (year % 4= =0 && year % 100! =0)
        {
            count++;
            printf("%d ", year);
        }
        else if (year % 400= =0)
        {
            count++;
            printf("%d ",year); }}printf("\n1000 to 2000, the number of leap years is :%d\n", count);
    return 0;
}
//count :243 

Copy the code

Error writing

// Error
int main(a)
{
    int count = 0;
    int year = 0;
    for(year = 1000; year <=2000; year++) {if (year % 4= =0  )
        {
           if(year % 100! =0)
           {
               count++;
               printf("%d ", year); }}else if (year % 400= =0)
        {
            count++;
            printf("%d ",year); }}printf("\n1000 to 2000, the number of leap years is :%d\n", count);
    return 0;
}
//count :240 
// There are three years missing: 1200 1600 2000
If (y%4==0) else if (y%4==0) else if (y%4==0
Copy the code

13. Programming problem: Print prime numbers within 100-200

Method 1: trial division

Prime number: not divisible by any number except 1 and itself

Determine if I is prime: Divide I by the number from 2 to i-1

// write method 1:
// Returns 1 for primes, 0 for non-primes
int is_prime(int n)
{
	int i = 0;
	for (i = 2; i < n; i++)
	{
		if (n %i == 0)
		{
			return 0; }}return 1;
}
/ / write 2
// Use a Boolean type -> reference to the header file stdbool
bool is_prime(int n)
{
	int i = 0;
		for (i = 2; i < n; i++)
		{
			if (n %i == 0)
			{
				return false; }}return true;
}
int main(a)
{
	int i = 0;
	int count = 0;
	for (i = 100; i <= 200; i++)
	{
		if (is_prime(i) == 1)
		{
			count++;
			printf("%d ", i); }}printf("The number of primes between \n100-200 is %d \n", count);
	return 0;
}
// There are 21 primes between 100 and 200
Copy the code

Method 2: Square it

If m is equal to a times b, at least one number of a and B is <=sqrt(m) such as:16 = 2*8 = 4*4As long as you have one number that divisible m you don't have to judge the other one so you just try to divide to the square root of msqrt() -> Square function references header file math.hCopy the code

int main(a)
{
	int i = 0;
	int count = 0;
	for (i = 100; i <= 200; i++)
	{
		int flag = 1;	// Assume a prime number
		int j = 0;
          // Try to divide to square I
		for (j = 2; j <=sqrt(i); j++)
		{
			
			if (i % j == 0)
			{
                // If divisible, it is not prime
				flag = 0;	
				break; }}if (flag == 1)
		{
			printf("%d ", i); count++; }}printf("The number of primes between \n100-200 is %d \n", count);
	return 0;
}
Copy the code

Method 3: optimization

Prime numbers only occur in odd numbers, so you can start with 101. Every time + = 2

int main(a)
{
	int i = 0;
	int count = 0;
	for (i = 101; i <= 200; i+=2)
	{
		int flag = 1;	// Assume a prime number
		int j = 0;
        // Try to divide to square I
		for (j = 2; j <=sqrt(i); j++)
		{
			
			if (i % j == 0)
			{
                // If divisible, it is not prime
				flag = 0;	
				break; }}if (flag == 1)
		{
			printf("%d ", i); count++; }}printf("The number of primes between \n100-200 is %d \n", count);
	return 0;
}
Copy the code

14. Program problem: find the minimum and maximum value of 10 numbers

// Error program
// Reason: set min and Max to 0, if we input negative values, the maximum value will be wrong
int main(a)
{
	int arr[10] = { 0 };
	int i = 0;
	// Enter elements for the array
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}
	int max = 0;
	int min = 0;
	for (int i = 0; i < 10; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
		if(arr[i] < min) { min = arr[i]; }}printf("min=%d max=%d", min, max);
	return 0;
}
Copy the code
/ / positive solutions
// Assume that an element in the array is the minimum. The maximum
int main(a)
{
	int arr[10] = { 0 };
	int i = 0;
	// Enter elements for the array
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}
	int max = arr[0];
	int min = arr[0];
	for (int i = 0; i < 10; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
		if(arr[i] < min) { min = arr[i]; }}printf("min=%d max=%d", min, max);
	return 0;
}
Copy the code
That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!