C brush questions (1)

Table of contents

1. Output multiples of 3 within 100

2. Output the 3 numbers from big to small

3. Print 100~200 prime numbers

method one

Method Two

4. Display the return value of printf

greatest common divisor

trial division

Roll and divide

Nine nine multiplication table

 Find the maximum of ten numbers


1. Output multiples of 3 within 100

 Method one:

int n = 0;
while (n*3 < 100)
	{
		printf("%d ", n*3);
		n++;
	}

Law 2:

	int i = 0;
	for (i = 1; i <= 100; i++) 
	{
		if (i % 3 == 0)
		{
			printf("%d ", i);
		}
	}

2. Output the 3 numbers from big to small

Since there are only 3 numbers, we can use the method of converting intermediate variables , which is very simple compared to sequential judgment.

	int a = 0;
	int b = 0;
	int c = 0;
	int tmp = 0;
	scanf("%d %d %d", &a, &b, &c);
	if (a < c)
	{
		tmp = c;
		c = a;
		a = tmp;
	}
	if (a < b)
	{
		tmp = b;
		b = a;
		a = tmp;
	}
	if (b < c)
	{
		tmp = b;
		b = c;
		c = tmp;
	}

	printf("%d %d %d\n", a, b, c);
	return 0; 
}

It should be noted that this method changes the value of the original variable.

3. Print 100~200 prime numbers

method one

Prime numbers, also known as prime numbers, are characterized by the fact that they can only be divisible by 1 and itself , so can we use the numbers between 2~n-1 to take the modulus for judgment? If there is a number that can be divisible evenly, then it is not a prime number, otherwise it is a prime number.

	int i = 101;
	int n;
	for (i = 101; i < 200; i++)
	{
		int flag = 0;//开关
		for (n = 2; n < i; n++)
		{
			if (i % n == 0)
			{
				flag = 0;
				break;
			}
			else flag = 1;
		}
		if(flag == 1)
			printf("%d ", i);	
	 }

 Because one of the numbers is not divisible, it does not mean that it is not a prime number, so we set a switch to switch between true and false in turn. If it can be divisible, it will be false, and if it cannot be divisible , it will be true.

Method Two

Any number that is not a prime number can be divided into  m*n form.

16 = 4*4 

16 = 2*8

If the number is squared, if it is not a prime number, there must be a number between 2 and √n that makes it a factor.

(Example: √18 = 3 (integer omits fraction)

             18 = 2*9 18 = 3*6    

Think about it, everyone, is this the truth? Doing so can reduce the number of traversals.

#include <stdio.h>
#include<math.h>
int main(){
    int i = 101;
	int n;
	for (i = 101; i < 200; i+2)
	{
		int flag = 0;
		for (n = 2; n <= sqrt(i); n++)
		{
			if (i % n == 0)
			{
				flag = 0;
				break;
			}
			else flag = 1;
		}
		if(flag == 1)
			printf("%d ", i);	
	 }
return 0;
}

Note that the factor can be equal to √n, s qrt is a function used to find the root sign, and the header file math.h needs to be included

Since an even number cannot be prime, we can skip the even part and only loop over the odd numbers.

4. Display the return value of printf

Requirements: Input the output value of printf in the first line, and output the return value of printf in the second line.

	int ret = printf("hello world!");
	printf("\n%d", ret);
//等价于
	int ret = printf("\n%d", printf("hello world!"));

 

From this we can conclude that the return value of printf is the number of characters 

greatest common divisor

trial division

We find the smaller of the two numbers and use it to decrease in turn. If it can be divisible by these two numbers, then it is the greatest common divisor.

    int a = 0, b = 0;
	scanf("%d %d", &a, &b);
	int ret = a > b ? b : a;

	while (a % ret || b % ret)
	{
		ret--;
	}
	printf("%d\n", ret);

Roll and divide

Specifically, the modulus of the two numbers is first calculated. If it is not equal to 0, the dividend is used as the next divisor, and their modulus is the new dividend until there is no remainder .

For example: 24%12 = 0, then 12 is the greatest common divisor. 25%12 = 1, 12%1 = 0, 1 is the greatest common divisor.

In turn, 12%24 = 12, 24%12 = 0, the method is still valid.

	int a = 0, b = 0;
	int r = 0;
	scanf("%d %d", &a, &b);
	while (r = a % b)
	{
		a = b;
		b = r;
	}
	printf("%d\n", b); 

 Additional question: What should I do if the least common multiple is required?

Here is a formula: least common multiple = product of two numbers divided by greatest common divisor (a*b/r)

Nine nine multiplication table

    int i = 0;
	for (i = 1; i <= 9; i++)
	{
		//打印一行
		int j = 0;
		for (j=1; j<=i; j++)//列数不超过行数
		{
			printf("%d*%d=%-2d ", i, j, i * j);
		}
		printf("\n");
	}

I won’t go into details here. Note that we use the left-aligned %-2d method here to make the code tidy. I hope you can also use this technique when writing code .

 Find the maximum of ten numbers

There is nothing to say about this. Suppose a variable max traverses the array and selects the maximum value. It should be noted that max can only be a value in the array . If it is given an initial value of 0, an error may occur if a negative number is encountered.

int arr[] = { -1,-2,-3,-4,-5,-6,-7,-8,-9,-10 };
	int max = arr[0];
	int i = 0;
	for (i = 1; i < 10; i++)//从第二个数开始
	{
		if (arr[i] > max)
			max = arr[i];
	}
	
	printf("%d\n", max);

Guess you like

Origin blog.csdn.net/dwededewde/article/details/131882560