当前位置:网站首页>Tan Haoqiang C language Fifth Edition chapter two exercise solution

Tan Haoqiang C language Fifth Edition chapter two exercise solution

2021-01-17 11:32:04 osc_ caf7pwkz

Analysis of the answer

4.1

 Insert picture description here
Their thinking
Just take out an empty bottle Vinegar or soy sauce That is, variable exchange

int main()
{
   
   
	int vinegar = 1;
	int sauce = 2;
	int empty=0;
	// take vinegar Pour into empty
	empty = vinegar;
	// hold sauce Pour into 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 };
	// adopt for loop jiang10 Enter the number into the array 
	for (i = 0; i < 10; i++)
	{
   
   
		 scanf("%d", &arr[i]);
	}
	

	k = arr[0];
	i = 0;
	// Let's set the first number to the largest   And then with each number in the array   Compare  
 	for (i = 0; i < 10; i++)
	{
   
   
		// If he's bigger than the chosen number   So give him the big number 
		if (arr[i] > k)
		{
   
   
			k = arr[i];
		}
	}
	printf("%d",k);
	return 0;
}

; The running 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 };
	// Enter the array elements in turn 
	for (i = 0; i < 3; i++)
	{
   
   
		scanf("%d", &arr[i]);
	}
	int m = 0;
	int n = 0;
	int temp;
	// By sorting algorithm   Arrange the elements in order from small to large 
	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;
			}
		}
	}
	// Output... In sequence 
	i = 0;
	for (i = 0; i < 3; i++)
	{
   
   
		printf("%d ", arr[i]);
	}
	return 0;
}

Attached is the 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  Cycle production  1-100 It's just an integer  
		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);
	// Judge   Divide 5 and 3 Whether the remainder of is also 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++;// Attach... To each element of the array through this 100-200
		for (j = 2; j < arr[i]; j++)
		{
   
   
			if (arr[i] % j == 0)// Judge arr[i] Is this element a composite number 
			{
   
   
				arr[i] = 0;// The sum is set to zero directly 
				break;
			}
	    }
	}
	int m = 0;
	for (m = 0; m < 101; m++)
	{
   
   
		if (arr[m] != 0)// Remove the zeroed elements 
		{
   
   
			printf("%d  ", arr[m]);
		}
	}
	return 0;
 }

 Insert picture description here

4.7

 Insert picture description here

Find the greatest common divisor Ancestral More subtraction
The first step in subtraction is to determine whether the greatest common divisor of two numbers is 2
We just need to put for The loop entry is set to **for (; i % 2 == 0&&j%2==0;)** Just the two numbers required i j All can be divisible at the same time 2 You can go into the cycle We need to add another variable m Sure, except a few times 2 In the follow-up add

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

When we type in i j variable The second step is to judge i j Who is big For convenience We will i The variable is set to a larger variable Through simple variable transformation

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

 Insert picture description here
It can be seen from this example that That is to use the larger input variable first i Minus the smaller variables j The difference is Then the difference is compared with the subtraction The larger ones are subtracted The small ones subtract Until the subtraction equals the difference
so The exit of the loop is that subtraction and difference are equal .
Design thinking Subtract first and then The greater value of the sum and difference of the subtraction is attached 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 m The variable is greater than 0 So the factor is multiplied by 2 Of m times On the contrary, direct output

if (m > 0)
	 {
   
   
		 n = 2 * m * b;
		 printf(" The greatest common divisor is %d", n);
      }
	 else
		 printf(" The greatest common divisor is %d", j);

Attach the entire

common()
 {
   
   
	 int a = 0;
	 int n = 0;
	 int m = 0;
	 int i = 0;
	 int j = 0; 
	 printf(" Please enter the evaluated number \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(" The greatest common divisor is %d", n);
      }
	 else
		 printf(" The greatest common divisor is %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(" Please turn the equation into standard form AX^2+BX+C=0");
	printf(" Please input... In turn A,B,C");
	scanf("%d %d %d", &i, &j, &k);
	m=j * j - 4 * i * k ;
	
	if (m < 0)
	{
   
   
		printf(" There are no real roots ");
	}
	n = sqrt(m);// Square root function 
	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++;
		// The first condition 
		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 says 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;// Ten numbers are passed into the array 
	for (i = 0; i < 10; i++)
	{
   
   
		scanf("%d", &arr[i]);
	}
	int m, n;
	for (m = 0; m < 10; m++)
	{
   
   // Bubble sort   Output the last one 
		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

版权声明
本文为[osc_ caf7pwkz]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210117112951209a.html