C Language: Exercises

Question 1: Singles

 

Only two numbers appear once in an array, and all other numbers appear twice.
Write a function to find the two numbers that occur only once.
For example:
the elements of the array are: 1, 2, 3, 4, 5, 1, 2, 3, 4,
6, only 5 and 6 appear once, to find 5 and 6.

#include <stdio.h>


int* find_signal_dog(int* arr,int sz,int* signal_dog)
{
	int i = 0;
	int tmp = 0;
	for (i = 0; i < sz; i++)
	{
		tmp ^= arr[i];//得到不相同的两个数的按位异或
	}
	int pos = 0;
	for (i = 0; i < 32; i++)
	{
		//得出向右移动多少位按位与结果为一
		if (((tmp >> i) & 1) == 1)
		{
			pos = i;
			break;
		}
	}
	for (i = 0; i < sz; i++)
	{
		//将&为一和零的各分一组,再异或得到不同的两个数
		if (((arr[i] >> pos) & 1) == 1)
		{
			signal_dog[0] ^= arr[i];
		}
		else
		{
			signal_dog[1] ^= arr[i];
		}
	}

}



int main()
{
	int arr[] = { 1,2,3,4,5,1,2,3,4,6 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int* signal_dog[2] = { 0 };
	find_signal_dog(arr, sz, signal_dog);
	printf("%d %d\n",signal_dog[0] , signal_dog[1]);
	return 0;
}

wrong multiple choice

There are the following macro definitions and structure definitions

When A=2, B=3, the pointer allocates ( ) bytes of space.

int main()
{
  unsigned char puc[4];
  struct tagPIM
  {
    unsigned char ucPim1;
    unsigned char ucData0 : 1;
    unsigned char ucData1 : 2;
    unsigned char ucData2 : 3;
  }*pstPimData;
  pstPimData = (struct tagPIM*)puc;
  memset(puc,0,4);
  pstPimData->ucPim1 = 2; 
  pstPimData->ucData0 = 3;
  pstPimData->ucData1 = 4;
  pstPimData->ucData2 = 5;
  printf("%02x %02x %02x %02x\n",puc[0], puc[1], puc[2], puc[3]);
  return 0;
}

A.20

B.15

C.11

D.9

Explanation: The structure is aligned to the longest char. The first two bit segment elements have a total of 4+2 bits, which are less than 8 bits. They occupy 1 byte in total, and the last one is 1 byte alone, a total of 3 bytes. In addition, #define performs search and replacement. The statement sizeof(struct _Record_Struct) * MAX_SIZE is actually 3*2+3, and the result is 9, so choose D.

Under X86, little-endian byte order storage, there are the following programs

#include<stdio.h>
int main()
{
  union
  {
    short k;
    char i[2];
  }*s, a;
  s = &a;
  s->i[0] = 0x39;
  s->i[1] = 0x38;
  printf("%x\n", a.k);
  return 0;
}

The output result is ( )

A.3839

B.3938

C.380039

D. not sure

The union has only 2 bytes, and the 2-byte hexadecimal has only 4 bits, so the answer CD is excluded. The bit order is similar to little endian, the low address is at the low position, so 39 is the low address, at the low position, and 38 is at the high position, so it is 3839, so choose A.

 classic topic

The result of the following code is ( )

int main()
{
  unsigned char puc[4];
  struct tagPIM
  {
    unsigned char ucPim1;
    unsigned char ucData0 : 1;
    unsigned char ucData1 : 2;
    unsigned char ucData2 : 3;
  }*pstPimData;
  pstPimData = (struct tagPIM*)puc;
  memset(puc,0,4);
  pstPimData->ucPim1 = 2; 
  pstPimData->ucData0 = 3;
  pstPimData->ucData1 = 4;
  pstPimData->ucData2 = 5;
  printf("%02x %02x %02x %02x\n",puc[0], puc[1], puc[2], puc[3]);
  return 0;
}

B.02 29 00 00

C.02 25 00 00

D.02 29 04 00

puc is a char array, which jumps one byte at a time, but the structure is not, it only has the first element to share one byte alone, and the other three elements share one byte together, so after puc is filled by the structure, it has only Two bytes will be written, the last two bytes must be 0, so far AD is excluded, and then the first byte is 2, it is 2, the second byte is more troublesome, first of all, ucData0 gave 3, which is actually out of bounds Yes, the 1-digit number can only be 0 or 1, so 11 is truncated to only 1. Similarly, the 4 given by ucData1 is also out of bounds. After 100 is truncated, it is 00, and only 5 of 101 is normal. The filling sequence is similar to the low address of the little endian, so the arrangement sequence is 00 101 00 1. That is 0010 1001, which is 0x29, so choose B.

Guess you like

Origin blog.csdn.net/weixin_71964780/article/details/132130443