Branch and loop exercises and knowledge points

Table of contents

knowledge points

programming

(1) Write the code to output the three integers in descending order. Input: 2 3 1 Output: 3 2 1

(2) Given two numbers, find the greatest common divisor of the two numbers

(3) Print the leap year between 1000 and 2000 to judge whether it is a leap year

(4) Print the prime numbers between 100 and 200

(5) Find the maximum value of 10 numbers

knowledge points

(1) Leap year: divisible by 4 && not divisible by 100 || divisible by 400.

(2) The while loop condition will be executed one more time than the loop body.

(3) 9*9 multiplication table, %3d is _23 (left alignment), %-3d is 23_ (right alignment) so it is "  %d*%d=%-3d" or " %d*%d= %d\t"  

programming

(1) Write the code to output the three integers in descending order. Input: 2 3 1 Output: 3 2 1

 Code display:

#include <stdio.h>
int main()
{
    int a = 2;
    int b = 3;
    int c = 1;
    scanf("%d%d%d",&a, &b,&c);
    if(a<b)
    {
        int tmp = a;
        a = b;
        b = tmp;
    }
    if(a<c)
    {
        int tmp = a;
        a = c;
        c = tmp;
    }
    if(b<c)
    {
        int tmp = b;
        b = c;
        c = tmp;
    }
    printf("a=%d b=%d c=%d\n", a, b, c);
    return 0;
}

Code understanding: The three numbers are relatively large, what we want is a>b, a>c, b>c. But the numbers will appear that we don’t want, so write three if statements to achieve the results we want That's it.

(2) Given two numbers, find the greatest common divisor of the two numbers

Code 1 shows: rolling and dividing method

#include <stdio.h>
int main()
{
	int a = 18;
	int b = 24;
	int c = 0;
	while (c = a % b)
	{
		a = b;
		b = c;
	}
	printf("%d\n", b);
	return 0;
}

The remainder of the dividend/divisor becomes the next divisor, and the divisor becomes the dividend. When divisible, the divisor at this time is the greatest common divisor.

Code 2 shows : (the greatest common divisor must be <= the smaller number of the two, first find the smaller number, and then subtract one in turn)

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	int i = 0;
	scanf("%d %d", &a, &b);
	if (a > b)
		c = b;
	else
		c = a;
	for (i = c; i >= 0; i--)
	{
		if ((a % i == 0) && (b % i == 0))
			break;
	}
	printf("%d", i);
	return 0;
}

Expansion: least common multiple=m*n/greatest common divisor

(3) Print the leap year between 1000 and 2000 to judge whether it is a leap year

Code display:

#include <stdio.h>
int main()
{
	int year = 0;
	for (year = 1000; year <= 2000; year++)
	{
		if (year % 4 == 0) 
		{
			if (year % 100 != 0)
			{
				printf("%d ", year);
			}
		}
		if (year % 400 == 0)
		{
			printf("%d ", year);
		}
	}
	return 0;
}

Code 2 shows:

#include <stdio.h>
int main()
{
	int year = 0;
	for (year = 1000; year <= 2000; year++)
	{
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		{
			printf("%d ", year);
		}
	}
	return 0;
}

(4) Print the prime numbers between 100 and 200

Code 1 shows:

#include <stdio.h>
int main()
{
	int i = 0;
	int j = 0;
	printf("100~200间的素数有:");
	for (i = 100; i <= 200; i++)
	{
		for (j = 2; j < i; j++)
		{
			if (i % j == 0)
				break;
		}
		{
			if (j == i)
				printf("%d ", i);
		}
	}
	return 0;
}

Code 2 shows:

#include <stdio.h>
#include <math.h>
int main()
{
	int i = 0;
	int j = 0;
	int shu = 1;
	printf("100~200间的素数有:");
	for (i = 101; i <= 200; i+=2)
	{
		shu = 1;
		for (j = 2; j <= sqrt(i); j++)
		{
			if (i % j == 0)
			{
				shu = 0;
				break;
			}

		}
		if (shu == 1)
			printf("%d ", i);
	}
}

Knowledge points: (1) If a number a = m * n, there must be a number less than equal to the square root of a

(2) Even numbers cannot have prime numbers

(3) sqrt(a) square root in C language <math.h>

(5) Find the maximum value of 10 numbers

Code 1 shows:

#include <stdio.h>
int main()
{
	int max = 0;
	int a = 0;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		if (i == 0)
		{
			scanf("%d", &a);
			max = a;
		}
		else
		{
			scanf("%d", &a);
			if (a > max)
				max = a;
		}
	}
	printf("%d", max);
	return 0;
}

Code 2 shows:

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}
	int max = arr[1];
	for (i = 0; i < 10; i++)
	{
		if (arr[i] > max)
			max = arr[i];
	}
	printf("%d", max);
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_57388581/article/details/125695397