当前位置:网站首页>Exercise analysis summary

Exercise analysis summary

2022-06-23 01:42:00 'Dream_

If you will It's better to start than to give up .     Roman.

May we all have our own goals and are making unremitting efforts for them .

-----------------------------------------------------------------------

1. Find the maximum of the three numbers .

//1. Find the maximum of the three numbers .
// Two by two   The biggest one is max in 

#include<stdio.h>
int main()
{
	// Method 1 : Utilization cycle 
	/*int i = 0;
	int num[3] = { 0 };
	printf(" Please enter any three integers :\n");
	for (i = 0; i < 3; i++)
	{
		scanf("%d", &num[i]);
	}
	int max = num[0];
	for (i = 1; i < 3; i++)
	{
		if (max < num[i])
		{
			max = num[i];
		}
	}
	printf("max = %d\n", max);*/

	// Method 2 : Direct input 
	int a = 0;
	int b = 0;
	int c = 0;
	printf(" Please enter any three integers :\n");
	scanf("%d %d %d", &a, &b, &c);
	int max = 0;
	max = (a > b) ? (a) : (b);
	max = (max > c) ? (max) : (c);
	printf("max=%d\n", max);
	return 0;
}

********************************************

2. The three integers are arranged in descending order .

//2. The three integers are arranged in descending order .
// Given a particular order :a b c( From big to small )
// Three pairwise comparisons 

#include<stdio.h>
int main()
{
	/*int a = 0;
	int b = 0;
	int c = 0;
	int tmp = 0;
	printf(" Please enter any three integers :\n");
	scanf("%d %d %d", &a, &b, &c);
	if (a < b)
	{
		tmp = a;
		a = b;
		b = tmp;
	}
	if (a < c)
	{
		tmp = a;
		a = c;
		c = tmp;
	}
	if (b < c)
	{
		tmp = b;
		b = c;
		c = tmp;
	}
	printf(" From large to small :%d %d %d\n", a, b, c);*/
	return 0;
}

******************************************

3、 Find the greatest common divisor of two numbers .

//3、 Find the greatest common divisor of two numbers .
// division 

#include<stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	printf(" Please enter any two integers :\n");
	scanf("%d %d", &a, &b);
	while (a % b != 0)
	{
		int c = a % b;
		a = b;
		b = c;
	}
	printf(" greatest common divisor :%d\n", b);
	return 0;
}

************************************************

4、 Find the least common multiple of two numbers .

//4、 Find the least common multiple of two numbers .
// a*b/ greatest common divisor 

#include<stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	printf(" Please enter any two integers :\n");
	scanf("%d %d", &a, &b);
	int mul = a * b;
	while (a % b != 0)
	{
		int c = a % b;
		a = b;
		b = c;
	}
	printf(" Minimum common multiple :%d\n", (mul / b));
	return 0;
}

****************************************

5、 Judge 100-200 The prime number in .

//5、 Judge 100-200 The prime number in .
// Mark   Optimize sqrt(i)

#include<stdio.h>
#include<math.h>//sqrt The header file 
int main()
{
	int i = 0;
	printf("100-200 The prime number in :\n");
	for (i = 101; i <= 200; i+=2)// Even numbers can't be prime numbers 
	{
		int flag = 1;// Pay attention to the position : Update each cycle 
		int j = 0;
		for (j = 2; j <= sqrt(i); j++)// Pay attention to the scope : At least one of the two multipliers is less than or equal to the square root value 
		{
			if (0 == i % j)
			{
				flag = 0;
				break;
			}
		}
		if (flag == 1)
		{
			printf("%d ", i);
		}
	}
	return 0;
}

*******************************************************

6、 seek 1/1-1/2+1/3-1/4....+1/99-1/100 Value .

//6、 seek 1/1-1/2+1/3-1/4....+1/99-1/100 Value .
// Be careful -1 Write method of 

#include<stdio.h>
#include<math.h>
int main()
{
	int i = 0;
	int j = 1;
	double sum = 0.00;
	for (i = 1; i <= 100; i++)
	{
		//sum += (double)((1.0 / i) * j);
		//j = -j;//-1 Input mode 1 

		//-1 Input mode 2 
		sum += (double)((pow(-1.0, (double)i + 1)) * (1.0 / i));
	}
	printf("1/1-1/2+1/3-1/4....+1/99-1/100 Value :%lf\n",sum);
	return 0;
}

--------------------------------------------------------------------------------------- Add

  • #include #define Not a keyword , It's preprocessing instructions
  • char Classified as an integer ,char Is the storage used ASCII code , and ASCII Code is an integer
  • When calculating “abcdef” For length strlen, The length calculation only includes ‘\0’ Number of all previous characters

---------------- All a person's anger comes from the pain of his incompetence .---------------------

原网站

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