c language exercises

1. Find the number of 1 in a binary number

Idea: binary numbers only have 0 and 1, and the result of bitwise sum with 1 is only 0 and 1. If there is 0 with logic, 0 will be 0, and all 1 will be 1. Therefore, binary numbers can be bitwise sum with 1 one by one by shifting to the right to get 1. Then your 1 bit is 1, so the code is as follows:

#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int get_one(int num)
{
	int i=0;
	int count=0;
	for(i=0;i<32;i++)  //32 bits in total, 31 bits to the right 
		if(((num>>i)&1)!=0)
		{
			count++;
		}
		return count;
}
int main ()
{
	int num=0;
	int ret =0;
	scanf("%d",&num);
	ret=get_one(num);
	printf("ret=%d",ret);
	return 0;
}

2. Print odd and even digits of a binary sequence

Idea: assuming that the number is - 1, then its binary sequence is 32 ones. We call each odd digit to move two bits backward and operate with 1 in turn. No matter whether you are 0 or 1, you will get your own number after operating with 1. Therefore, even digits can also be operated in this way. The code is as follows:

#include <stdio.h>
void print(int num)
{
	int i=0;
	int j=0;
	printf("Odd bits are\n");
	for(i=30;i>=0;i-=2)   //Odd bits from bit 1 to bit 31
	{
		printf("%d ",(num>>i)&1);
	}
	printf("\n");
	printf("Even bits are\n");
	for(j=31;j>=1;j-=2)  //Even bits from bit 2 to 32
	{
		printf("%d ",(num>>i)&1);
	}
	printf("\n");
}
int main ()
{
	int num=0;
	scanf("%d",&num);
	print(num);
	return 0;
}

3. Give the binary expression of two int (32-bit) integers m and n, and find how many bits are different?

Idea: the simplest method is simply to XOR m and n. the same 0 and different 1. How many digits are different when there are several 1's, so it's still a number of 1's in the number after seeking the difference or. Here, you don't need to move right and do it in the order of bit and. Here is a new idea:

  Assuming the following conditions, given n=10, the binary sequence is 1010, and given n-1=9, the binary sequence is 1001; Carry out bitwise sum for both, and give the value after bitwise sum to N, and then carry out bitwise sum for N and the new n-1 again. You will find that the result will always become 0, and you will find that there are several 1s in your n, so you can do this operation several times.

n=1010n-1=1001n&n-1=1000
n=1000n-1=0111n&n-1=0000

The code is as follows:

4. Five divers said:

Player A said: B second, I third;
Player B said: I'm second, E is fourth;
Player C said: I'm first, D is second;
Player D said: C finally, I'm third;
Player E said: I'm fourth, A is first;
After the competition, each contestant is half right. Please program to determine the ranking of the competition.

Idea:

The truth can be regarded as 1; The lie is 0; The title means that if everyone is half right and each person says two sentences, it can be considered that the sum of the two sentences is 1. If you want to get the real ranking, that is, if all the words are combined into 1, you will get the real ranking. However, there are many cases of ranking after screening, because there are repetitions. The real answer must be only the first to fifth places, and the ranking multiplied by 120, Can be used as the final filter condition. The code is as follows:

#include<stdio.h>
int main ()
{
	int A=0;
	int B=0;
	int C=0;
	int D=0;
	int E=0;
	for (A=1;A<=5;A++)  //List A
	{
		for (B=1;B<=5;B++)//List B
		{
			for (C=1;C<=5;C++)//List C
			{
				for(D=1;D<=5;D++)//List D
				{
					for(E=1;E<=5;E++)//List E
					{
						if(((B==2)+(A==3)==1)&&  //True is 1 and false is 0. If everyone is right, it means that what everyone says adds up to 1
							((B==2)+(E==4)==1)&&  //All together, if it is 1, it means it is the real ranking 
							((C==1)+(D==2)==1)&&
							((C==5)+(D==3)==1)&&
							((E==4)+(A==1)==1))
						{
							if(A*B*C*D*E==120)  //In all cases, it is possible that the ranking will be repeated, so it can be judged by multiplying the ranking
							{
								printf("A=%d B=%d C=%d D=%d E=%d\n",A,B,C,D,E);
							}
						}
					}
				}
			}
		}
	}
	return 0;
}

5. Write a function to realize the K-th string in the right-hand string

Suppose a string is required to implement abcdefg, turn right 3 characters and become efgabcd;  

Idea:

The subscripts are 0 to 6, that is, 0 to the number of elements minus 1. You can consider first giving the rightmost character to a temporary variable, then moving the next character to the right, and then the left position is empty. Then you can put the character of the temporary variable on the leftmost, so as to turn right. The code is as follows:

#include <stdio.h>
#include <string.h>
#include <assert.h>
void right_arr(char* arr,int k)
{
	int i=0;
	char tmp=0;
	int len=0;
	assert(arr!=NULL); //Assert the pointer after defining the variable to prevent dereference of null pointers
	len=strlen(arr);
	for (i=0;i<k;i++) //To flip k characters, loop k times
	{
		int j=0;
		tmp=*(arr+len-1);  //The last element is subscripted arr+len-1, and the last element is placed in a temporary variable
		for (j=0;j<len-1;j++)  //J < len will cross the boundary 
		{
			*(arr+len-1-j)=*(arr+len-2-j);  //Put the element of arr+len-2 at the position of arr+len-1 and perform a cycle to subtract j
		}  
		*(arr)=tmp;  //Turn right by putting the value of the temporary variable first
	}
}
int main ()
{
	char arr[]="abcdefg";
	right_arr(arr,3);
	printf("%s\n",arr);
	return 0;
}

5. Write a function to realize the K-th string in the left-handed string

Suppose a string is required to implement abcdefg to turn left, and three characters become defgabc;  

This is a problem with the right turn string, but here we use another method to realize left rotation. We can consider first turning the character abc you want to turn left into cba in reverse order,   Next, turn the right side of the reverse order into gfed, and now the string into cbagfed, and then turn the overall reverse order into defgabc, that is, turn the left rotation problem into a triple reverse order problem. The reverse order problem is very simple, that is, exchange the first element and the tail element. The triple reverse order can be encapsulated by a function. The code is as follows:

#include <stdio.h>
#include <string.h>
#include <assert.h>
void reverse(char* left,char *right) //Define left and right pointers to the first and last elements of the string in reverse order each time
{
	char tmp=0;
	assert (left);
	assert (right);  //Come in and assert that the two pointers are not NULL
	while (left <right)
	{
		tmp = *left;
		*left= *right;
		*right=tmp;
		left++;
		right--;
	}

}
void left_arr(char* arr,int k)
{
	int len=0;
	len=strlen(arr);
	assert(arr);
	assert(k<len);  //Assert that the number you want to rotate left will not be greater than the length of the string itself
	reverse(arr,arr+k-1);//If there are three left-handed characters on the left side of reverse order, the mantissa subscript on the left side of reverse order is arr+k-1
	reverse(arr+k,arr+len-1);//Reverse right  
	reverse(arr,arr+len-1);//Inverse whole
}
int main ()
{
	char arr[]="abcdefg";
	left_arr(arr,3);
	printf("%s\n",arr);
	return 0;
}

6. Write a function to judge whether a string is a rotated string of another string

Idea: suppose there is a string abcdefg. The most violent way to judge whether a string is another string after rotation is to enumerate all the situations after rotation and compare them with the strings to be compared, which meets the meaning of the question. Here is another method. If an abcdefg is added after abcdefg, that is, abcdefgabcdefg, Here, the edge has completely covered all the situations after rotation. You only need to judge whether the string you want to judge is a substring of the appended string. It is a substring, which meets the meaning of the question. Here, the strncat function can be used to append strings, and the strstr function can be used to judge substrings. These are library functions. The code is as follows:

#include <stdio.h>
#include <string.h>
int is_left_arr(char* str1,char* str2)
{
	int len=0;
	char* ret=NULL;
	len=strlen (str1);
	strncat (str1,str1,6);  //You can't use strcat by appending after yourself
	ret=strstr(str1,str2);
	if (ret ==NULL)  //ret returns a null pointer indicating that it is not a substring
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
int main ()
{
	int ret =0;
	char arr1[30]="abcdef";
	char arr2[]="cdefab";
	ret=is_left_arr(arr1,arr2);

	if (ret ==1)
	{
		printf ("Y\n");
	}
	else 
	{
		printf("N\n");
	}
	return 0;
}

7. Find a number in the young's matrix. The young's matrix increases each row element and each column element

Idea:

According to the definition of Young's matrix, the elements with upper right corner and lower left corner are very special. For example, the element in the upper right corner is the largest element in a row and the smallest element in a column. Suppose that the matrix is

1 2 3

4 5 6

7 8 9   Find 5. If 5 is larger than element 3 in the upper right corner, it means that it is not in the first row, so the element in the first row can be ignored directly. Now the element in the upper right corner is 6,6 > 5, and 6 is the smallest element in the column, it means that there is no 5 in the column where 6 is located, so go to the left, which is similar, Until your row subscript is smaller than the number of rows minus 1 or equal to, and the column subscript is greater than or equal to 0, the search ends. The code is as follows:

#include <stdio.h>
int FindNum(int arr[3][3],int k,int row,int col)
{
	int x=0;
	int y=col -1;//The subscript of the upper right element is column - 1
	while (x<=row-1&& y>=0)
	{
		if (arr[x][y]>k)  
		{
			y--;
		}
		else if (arr[x][y]<k)
		{
			x++;
		}
		else
		{
			printf("The subscript is%d %d\n",x,y);
			return 1;
			
		}
	}
	return 0;
}

int main ()
{
	int arr[3][3]={{1,2,4},{3,5,6},{4,7,9}};
	int k=5;
	int ret =0;
	ret =FindNum(arr,k,3,3);
	if (ret ==1)
	{
		printf("eureka\n");
	}
	else 
		printf("can't find\n");
	return 0;
}

end:

It only represents a little summary of doing exercises after learning. Due to the limitations of knowledge, there must be many problems. I hope you guys don't spray. Thank you!

Tags: C

Posted on Tue, 30 Nov 2021 18:36:17 -0500 by T2theC