C brush questions (2)

Table of contents

Addition and subtraction mixed operations

Calculate n to the kth power

Computes the sum of bits of a non-negative integer

string reverse order

double pointer 

recursion

matrix calculation

 Matrix transpose


Addition and subtraction mixed operations

Title: Calculate the value of 1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 ... + 1 / 99 - 1 / 100, and print out the result.

In general, we can write a loop and then use conditional judgment statements to judge the two situations, but here is a better way to set a switch variable to switch between positive and negative numbers.

    int flag = 1;
	double sum = 0;
	for (int i = 1; i < 100; i++)
	{
		sum += flag* (1.0 / i);//注意小数
			flag = -flag;
	}
	printf("%lf", sum);

When calculating a fraction, at least one of the divisor and the dividend must be a decimal, otherwise it will be treated as an integer

Calculate n to the kth power

Three cases (recursive)

  • n < 0, 1.0 / Pow(n, -k) (becoming reciprocal after using forward recursion)
  • n > 0, n* Pow(n,k-1)
  • n = 0, 1
double Pow(int n, int k)
{
	if (k < 0)
	{
		return 1.0 / n * Pow(n, -k);
	}
	else if (k == 0)
	{
		return 1;
	}
	else return n * Pow(n, k - 1);
}
int main()
{
    int n = 0;
	int k = 0;
	scanf("%d %d", &n, &k);
	double ret = Pow(n, k);
	printf("%lf\n", ret);
	return 0;
}

Computes the sum of bits of a non-negative integer

Use recursion to split from back to front until there is only one digit.

int DigitSum(unsigned int n)
{
	if (n < 10)
		return n;
	else
        return DigitSum(n / 10) + n % 10;
}
int main()
{
	unsigned int num = 0;
	scanf("%u", &num);
	int sum = DigitSum(num);
	printf("%d\n", sum);
	return 0;
}

string reverse order

Given a character hypothesis abcdef, invert to fedcba.

double pointer 

Set the head and tail pointers, move closer to the middle after swapping.

//下标
void reverse_string(char* str)
{
	int len = strlen(str);
	int left = 0;
	int right = len - 1;

	while (left<right)
	{
		char tmp = *(str + left);
		*(str + left) = *(str + right);
		*(str + right) = tmp;
		left++;
		right--;
	}
}
//指针
void reverse_string(char* str)
{
	int len = strlen(str);
	char* left = str;
	char* right = str + len - 1;

	while (left<right)
	{
		char tmp = *left;
		*left = *right;
		*right = tmp;
		left++;
		right--;
	}
}

recursion

Recursion may not be easy to think of, because it is recursive from both ends to the middle , and the end condition is that there is only one number left or no number can be exchanged . How can recursion go both ways? This will use our \0 . When the number on the left changes to the right, don’t rush to change the number on the left to the right, but add a \0 to the right position, and then add 1 to the pointer to form recursion , so that when exchanging, there is no need to consider the exchanged content after \0 , so as to achieve correct inversion.

The figure above shows the specific process of recursion. As can be seen from the figure, the length of the array is calculated from manually replacing \0 (if the condition is not met, replace \0 with a number). The judgment condition is that the length of the array is greater than 1 to meet the recursion condition, but this is the case of an even number . If it is an odd number , the length is 4. After one recursion, it becomes 2. After that, the recursion condition is not satisfied, so the correct judgment condition should be that the array length is greater than 2

void reverse_string(char* str)
{
	int len = strlen(str);
	char* tmp = *str;
	*str = *(str + len - 1);
	*(str + len - 1) = '\0';
	if (*str > 2)
	{
		reverse_string(str + 1);
	}
	*(str + len - 1) = 'tmp';
}

matrix calculation

 Matrix printing uses a two-dimensional matrix, input the value of the matrix in the inner layer, and judge the number greater than 0 to accumulate. 

    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int i = 0;
    int k = 0;
    int sum = 0;
	for (i = 0; i < n; i++)
	{
		int j = 0;
		for (j = 0; j < m; j++)
		{
			scanf("%d", &k);
			if (k >= 0)
				sum += k;
		}
	}
		printf("%d\n", sum);

 Matrix transpose

Note that the transpose needs to store the value in a two-dimensional array first, and then change from printing by row to print by column.

	int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    //int arr[n][m];//c99
    int arr[10][10];
    int i = 0;
    int j = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
	for (j = 0; j < m; j++)
	{
		for (i = 0; i < n; i++)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}

If on some question brushing websites, we can directly use variable-length arrays to initialize the arrays. If we do not use variable-length arrays, we can directly set the array size to the maximum length specified in the question.

Supongo que te gusta

Origin blog.csdn.net/dwededewde/article/details/132084587
Recomendado
Clasificación