C language: Brush question summary

Posted by Hardwarez on Thu, 24 Feb 2022 16:22:17 +0100

C language learning has come to an end. This paper is a summary of the recent learning of C language. Most of the questions come from niuke.com.

Topic 1: integer conversion

Title Description: write A function to determine how many bits need to be changed to convert integer A to integer B.

Idea: in fact, this problem is actually to find how many 1s each bit in the A and B XOR result binary contains.

Implementation code:

#include <stdio.h>

int convertInteger(int A, int B)
{
	int k = A ^ B;
	int sum = 0;
	int i = 0;
	for (i = 0; i < 32; i++)
	{
		if (((k >> i) & 1) == 1)
		{
			sum++;
		}
	}
	return sum;
}

int main()
{
	int A = 0;
	int B = 0;
	scanf("%d %d", &A, &B);
	int ret = convertInteger(A, B);
	printf("%d\n", ret);
	return 0;
}

Topic 2: intersection of two arrays

Title Description: given two arrays nums1 and nums2, return their intersection. Each element in the output result must be unique. The order of output results is not considered.

Idea: traverse the two arrays. If the two elements in the two arrays are equal, save the number to avoid repeated search of the same element after traversal, resulting in wrong output results. Then, saving the equal elements here can define an array. The index of the array is the number of the intersection of these arrays. The next time you access the following table, you will not repeat the search.

Implementation code:

#include <stdio.h>

int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize)
{
	static int arr[1000];
	int brr[1000] = { 0 };
	int i = 0;
	int j = 0;
	*returnSize = 0;
	for (i = 0; i < nums1Size; i++)
	{
		for (j = 0; j < nums2Size; j++)
		{
			if (nums1[i] == nums2[j])
			{
				if (brr[nums1[i]] == 0)
				{
					brr[nums1[i]] = 1;
					arr[*returnSize] = nums1[i];
					(*returnSize)++;
					break;
				}
			}
		}
	}
	return arr;
}

int main()
{
	int nums1[] = { 4,9,5 };
	int nums2[] = { 9,4,9,8,4 };
	int nums1Size = sizeof(nums1) / sizeof(nums1[0]);
	int nums2Size = sizeof(nums2) / sizeof(nums2[0]);
	int returnSize = 0;
	int* ret = intersection(nums1, nums1Size, nums2, nums2Size, &returnSize);
	for (int i = 0; i < returnSize; i++)
	{
		printf("%d ", *(ret + i));
	}
	return 0;
}

Topic 3: sorting strings

Title Description: Enter: "a" to "Z", "a" to "Z", "0" to "9". Enter no more than 1024 letters or numbers. Output in descending order.

Idea: use qsort function to sort strings. This function has been described in detail in the previous article. See the previous article for details.

Implementation code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int cmp(const void* e1, const void* e2)
{
	return *(char*)e1 - *(char*)e2;
}

int main()
{
	char arr[1000] = { 0 };
	while (scanf("%s", arr) != EOF)
	{
		int sz = strlen(arr);
		qsort(arr, sz, sizeof(arr[0]), cmp);
		printf("%s\n", arr);
	}
	return 0;
}

Title 4: find the central subscript of the array

Title Description: give an integer array {nums to calculate the central subscript of the array.
                  1. The central subscript of the array is a subscript of the array. The sum of all elements on the left is equal to the sum of all elements on the right.
                  2. If the central subscript is at the leftmost end of the array, the sum of the numbers on the left is regarded as 0 because there is no element to the left of the subscript.
                  3. The same applies when the central subscript is at the rightmost end of the array.
                  4. If the array has multiple central subscripts, the one closest to the left should be returned. Returns - 1 if the array does not have a central subscript.

Idea: in this topic, all the subscripts in the array may be the central subscript of the array, so we should traverse the entire array to find the central subscript. In this way, when the array has multiple central subscripts, we will find the central subscript of the array on the far left. Among them, in the cycle of traversing all the subscripts of the array, Add up all the elements on the left and right of the subscript respectively, and return the subscript when the sum of the elements on the left is equal to the sum of the elements on the right.

Implementation code:

#include <stdio.h>

int pivotIndex(int* nums, int numsSize)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < numsSize; i++)
	{
		int sum1 = 0;
		int sum2 = 0;
		for (j = 0; j < numsSize; j++)
		{
			if (j < i)
			{
				sum1 += nums[j];
			}
			else if (j > i)
			{
				sum2 += nums[j];
			}
		}
		if (sum1 == sum2)
		{
			return i;
		}
	}
	return -1;
}

int main()
{
	int nums[] = { -1,-1,-1,-1,-1,0 };
	int numsSize = sizeof(nums) / sizeof(nums[0]);
	int ret = pivotIndex(nums, numsSize);
	printf("%d\n", ret);
	return 0;
}

Title 5: Statistics of the number of characters

Title Description: enter a line of string without spaces. Output the number of characters in the input string in the range of (0 ~ 127, including 0 and 127).

Idea: traverse each character of the input string, create an array, and set the character as the subscript of the array to judge whether the character has appeared before, and then judge the number of characters. The general idea is similar to the method of finding whether the intersection of the two arrays is repeated.

Implementation code:

#include <stdio.h>

int Count(char* ch)
{
	int count = 0;
	char arr[128] = { 0 };
	while (*ch != '\0')
	{
		if (arr[*ch] != 1)
		{
			count++;
		}
		arr[*ch] = 1;
		ch++;
	}
	return count;
}

int main()
{
	char ch[500] = { 0 };
	scanf("%s", ch);
	int ret = Count(ch);
	printf("%d\n", ret);
	return 0;
}

Topic 6: find most elements in an array

Title Description: given an array of size n, find most of its elements. Most elements refer to elements that appear more than n/2 in the array. Suppose that the array is non empty, and there are always many elements in a given array.

Idea: a number in an array appears more than n/2 times. Starting from the 0th character, it is assumed to be the most number. If it encounters the same number, it will be counted
Count + 1. If there is a difference, count - 1. In fact, they consume each other. When the count is 0, it means that the mutual spelling is completed and reopened from the next character
It starts to spell each other, but in the final analysis, the number that appears more than n/2 is more, so it is also the last reserved character.

Implementation code:

#include <stdio.h>

int majorityElement(int* nums, int numsSize)
{
	int count = 1;
	int tmp = nums[0];
	int i = 1;
	for (i = 1; i < numsSize; i++)
	{
		if (tmp == nums[i])
		{
			count++;
		}
		else
		{
			count--;
			if (count == 0)
			{
				tmp = nums[i + 1];
			}
		}
	}
	return tmp;
}

int main()
{
	int nums[] = { 1,2,2,2,3 };
	int numsSize = sizeof(nums) / sizeof(nums[0]);
	int ret = majorityElement(nums, numsSize);
	printf("%d\n", ret);
	return 0;
}

Topic 7: self divisor

Self divisor refers to the number that can be divided by every digit it contains. Given two integers left and right, a list is returned. The elements of the list are all the divisors in the range [left, right].

Idea: traverse the number between left and right, cycle the number among them, modulo 10 each time (that is, take the last bit of the number every time), judge it, and define an array to store these self divisor.

Implementation code:

#include <stdio.h>

int* selfDividingNumbers(int left, int right, int* returnSize)
{
	static int arr[10000] = { 0 };
	*returnSize = 0;
	int i = 0;
	for (i = left; i <= right; i++)
	{
		int j = i;
		int jude = 1;
		while (j)
		{
			int n = j % 10;
			if (n == 0)
			{
				jude = 0;
				break;
			}
			if (i % n != 0)
			{
				jude = 0;
				break;
			}
			j = j / 10;
		}
		if (jude == 1)
		{
			arr[*returnSize] = i;
			(*returnSize)++;
		}
	}
	return arr;
}

int main()
{
	int left = 47;
	int right = 85;
	int returnSize = 0;
	int* ret = selfDividingNumbers(left, right, &returnSize);
	int i = 0;
	for (i = 0; i < returnSize; i++)
	{
		printf("%d ", *(ret + i));
	}
	return 0;
}

Topics: C Back-end