Tan Haoqiang C language fifth edition chapter 2 exercises solutions

Answer analysis

4.1

Insert picture description here
Problem-solving ideas
just need to take out an empty bottle to hold vinegar or soy sauce to exchange variables

int main()
{
    
    
	int vinegar = 1;
	int sauce = 2;
	int empty=0;
	//将vinegar倒入empty
	empty = vinegar;
	//把sauce倒入vinrgar
	vinegar = sauce;
	sauce=empty;
	return 0;
}

4.2

Insert picture description here

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    
    
 	int i, j, k = 0;
	int arr[10] = {
    
     0 };
	//通过for循环jiang10个数录入数组中
	for (i = 0; i < 10; i++)
	{
    
    
		 scanf("%d", &arr[i]);
	}
	

	k = arr[0];
	i = 0;
	//假设将第一个数设为最大的 然后与数组每个数 比较 
 	for (i = 0; i < 10; i++)
	{
    
    
		//如果他比选定的数大 那么将大数赋给他
		if (arr[i] > k)
		{
    
    
			k = arr[i];
		}
	}
	printf("%d",k);
	return 0;
}

; The result of the code is shown in the figure

Insert picture description here

4.3

Insert picture description here

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    
    
	int tmp = 0;
	int i = 0;
	int arr[3] = {
    
     0 };
	//依次录入数组元素
	for (i = 0; i < 3; i++)
	{
    
    
		scanf("%d", &arr[i]);
	}
	int m = 0;
	int n = 0;
	int temp;
	//通过排序算法 将元素按从小到大的顺序排好
	for (m = 0; m < 3; m++)
	{
    
    
		for (n = 0; n < 3 - m - 1;n++)
		{
    
    
			if (arr[n] > arr[n + 1])
			{
    
    
				temp = arr[n];
				arr[n] = arr[n + 1];
				arr[n + 1] = temp;
			}
		}
	}
	//依次输出
	i = 0;
	for (i = 0; i < 3; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
	return 0;
}

Attach output diagram
Insert picture description here

4.4

Insert picture description here

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    
    
	int i, j; 
		j = 0;
		//for 循环产生 1-100个整数即可 
		for (i = 0; i < 101; i++)
		{
    
    
			j = i + j;
        }
		printf("%d", j);
		return 0;
}

4.5

Insert picture description here

#include<stdio.h>
int main()
{
    
    
	int i = 0;
	scanf("%d", &i);
	//判断 除以5和3的余数是否同时为0
	if (i%5==0&&i%3==0)
	{
    
    
		printf("yes");
	}
	else
		printf("no");
	return 0;
}

4.6

Insert picture description here

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    
    
	int i, j, k;
	k = 100;
	int arr[101] = {
    
     0 };
	for (i = 0; i < 101; i++)
	{
    
    
		arr[i] = k++;//通过这个给数组每个元素附上100-200
		for (j = 2; j < arr[i]; j++)
		{
    
    
			if (arr[i] % j == 0)//判断arr[i]这个元素是否是合数
			{
    
    
				arr[i] = 0;//合数直接置零
				break;
			}
	    }
	}
	int m = 0;
	for (m = 0; m < 101; m++)
	{
    
    
		if (arr[m] != 0)//排除掉被置零的元素
		{
    
    
			printf("%d  ", arr[m]);
		}
	}
	return 0;
 }

Insert picture description here

4.7

Insert picture description here

Seeking the greatest common divisor of the ancestor's more
subtraction technique The first step of the more subtraction technique is to determine whether the greatest common divisor of two numbers is 2,
we only need to set the for loop entry to **for (; i% 2 == 0&&j%2==0;)**As long as the required two numbers ij can be divisible by 2 at the same time, you can enter the loop. We also need to add a variable m to make sure that in addition to a few times, 2 will be added later

for (; i % 2 == 0&&j%2==0;)
	 {
    
    
		 i = i/2;
		 j = j / 2;
		 m++;
	 }

When we input the ij variable, the second step is to determine who is greater than ij. In order to facilitate us to set the i variable to a larger variable through simple variable conversion

if (j > i)
	 {
    
    
		 a = i;
		 i = j;
		 j = a;
	 }

Insert picture description here
It can be seen from this example that the difference obtained by subtracting the smaller variable j from the larger variable i of the input is used, and then the difference is larger than the minus, and the smaller the minus is the minus until the minus and the difference are equal and
visible. The exit of the cycle is that the subtraction and the difference are equal.
The design idea is to subtract first by attaching the larger value of the subtraction and difference to i

for (;;)
	 {
    
    
		 b = i - j;
		 
		
		 if (j == b)
		 {
    
    
			 break; 
		 }
		 if (b > j)
		 {
    
    
			 a = j;
			 j = b;
			 b = a;
		 }
		 i = j;
		 j = b;

     }

Finally, if the m variable is greater than 0, the factor must be multiplied by 2 m times, otherwise directly output

if (m > 0)
	 {
    
    
		 n = 2 * m * b;
		 printf("最大公约数是%d", n);
      }
	 else
		 printf("最大公约数是%d", j);

Attach the whole

common()
 {
    
    
	 int a = 0;
	 int n = 0;
	 int m = 0;
	 int i = 0;
	 int j = 0; 
	 printf("请输入被求数\n");
	 scanf("%d %d", &i, &j);
	 for (; i % 2 == 0&&j%2==0;)
	 {
    
    
		 i = i/2;
		 j = j / 2;
		 m++;
	 }
	 if (j > i)
	 {
    
    
		 a = i;
		 i = j;
		 j = a;
	 }
	 a = 0;
	 int b = 0;
	 for (; ; )
	 {
    
    
		 b = i - j;
		 
		
		 if (j == b)
		 {
    
    
			 break;
		 }
		 if (b > j)
		 {
    
    
			 a = j;
			 j = b;
			 b = a;
		 }
		 i = j;
		 j = b;

     }
	 if (m > 0)
	 {
    
    
		 n = 2 * m * b;
		 printf("最大公约数是%d", n);
      }
	 else
		 printf("最大公约数是%d", j);
 }

4.8

Insert picture description here

4.8.1

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int i, j, k, m;
	float n,p,q;
	printf("请将方程化成标准形式AX^2+BX+C=0");
	printf("请依次输入A,B,C");
	scanf("%d %d %d", &i, &j, &k);
	m=j * j - 4 * i * k ;
	
	if (m < 0)
	{
    
    
		printf("没有实数根");
	}
	n = sqrt(m);//平方根函数
	p = (n - j) / (2 * i);
	q = (-n - j) / (2 * i);
	printf("%.2f %.2f",p,q);

}

8.1

Insert picture description here

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int arr[101] = {
    
     0 };
	int i, j, k;
	k = 1900;
	for (i =0; i < 101; i++)
	{
    
    
		arr[i] = k++;
		//第一个条件
		if (arr[i] % 4 == 0 && arr[i] % 100 != 0)
		{
    
    
			printf("%d ", arr[i]);
		}
		if (arr[i] % 400 == 0 && arr[i] % 100 == 0)
		{
    
    
			printf("%d ", arr[i]);
		}
	}
	return 0;
}

Insert picture description here

8.2

It has been written above, see 4.8

8.3

Insert picture description here

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int arr[10] = {
    
     0 };
	int i, j, k;//十个数传入数组
	for (i = 0; i < 10; i++)
	{
    
    
		scanf("%d", &arr[i]);
	}
	int m, n;
	for (m = 0; m < 10; m++)
	{
    
    //冒泡排序 输出最后一个即可
		for (n = 0; n < 10 - m - 1; n++)
		{
    
    
			if (arr[n] > arr[n + 1])
			{
    
    
				k=arr[n];
				arr[n ] = arr[n+1];
				arr[n + 1] = k; 

			}
		}
	}
	printf("%d", arr[9]);
	return 0;
}


Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45849625/article/details/112654765