Supplement to interview questions of 2019 Xiyou linux group

Keywords: C

1. The first question is an input judgment, in which the Classl code is used, something related to the address

#include<stdio.h>
#include<string.h>
struct A{
	long a;
	int b;
};
int main(){
	struct A num ={0x6e694c756f796978};
	char ch='0';
	for(int i=0;ch;i++){
		ch=*((char *)&num+i);
		printf("%c",ch);

	}
	printf("\n");
	return 0;
}

I made a modification to judge whether it will be reversed (similar to this year's question). If there is no result, it means that it is normal positive order input, and the ASClL code of characters is input

2. Rewrite the program and use more methods to change b without changing const

#include<stdio.h>
int main(){
	const int b=10;
	/*
	*/
	return 0;
}

#include <stdio.h>
 
int main()
{
    const int a = 10;
     int *p =(int *) &a;
    (*p)++;
    printf("%d\n", a);
    return 0;
}

If the pointer itself is a constant, it cannot be modified, and the content pointed to by the pointer is a constant and cannot be modified.

Then I can change the address of its address. Here, pay attention to type conversion when assigning the address of a to the pointer, otherwise an error will be reported.

That is, it may be said that the initialization lost the qualification of the pointer target type

3. 

struct node {
*(int*)((char*)a+2*sizeof(structn))=100;
};

struct n{
char  a;
int b;
};

intmain(intargc,char*argv[]){
struct node a[2]={{1,2,3},{4,5,6}};
*(int *)((char *)a+2*sizeof(struct n))=100;
return 0;
}

The program has two structural variables,      struct node a[2] = {{1,2,3},{4,5,6}}; This sentence defines a structure array, generates 48 bytes of space in the next sentence, and then modifies the a after 32 bits to change it, so

The last bit in a changes to {{1, 2, 3}, {4, 5100}}

4.j explain the problem of function call

#include<stdio.h>
#define f(a,b) a##b 
#define g(a) #a
 #define h(a) g(a) 
 int main(int argc, char* argv[]) {
  printf("%s\n", h(f(1,2)));
   printf("%s\n", g(f(1,2)));
    return 0;
	 }

The important thing of this program is to start defining three macros, namely

 #define f(a,b) a##b 
#define g(a) #a
 #define h(a) g(a) 

Here, through analysis, we can get that the first is to change the existence similar to f(a,b) in the later stage into the form of ab. the second is to define an existence such as G (a), release the data as long as it is encountered, and finally define two functions g (a) and H (a). It can also be obtained that in the first output, the external is not g (a), so the internal output will be. At the same time, because ## of the existence, all symbols in it will be deleted, and only 12 will be output. In the second printf, the first is the same, but the external is g (x), so a modification will be made to output according to the format, so f (1,2) will be obtained

5.

lala and nini are good friends. One day lala got n cakes of different quality. lala wanted to share them with nini
Enjoy, let two people eat the same quality cake. But lala with obsessive-compulsive disorder doesn't want to destroy the integrity of each cake
Please write a program to judge whether these n cakes meet the requirements of lala.
This problem can be sorted first, and the period can be sorted from small to large, and then only part of the elements need to be summed. In the case of using the function, it can be summed for multiple rounds, and when half of the total quality occurs, it can exit the loop. For output, of course, you can also set a variable to accumulate when the judgment is established, which is very important In this way, I can judge whether the situation I need occurs according to the size of the variable.
6.
Write functions to achieve the following functions:
Input: one line string
Output: output the words in the string in reverse order, leaving no spaces at the beginning and end, and only one between adjacent words
Space
Sample( ❐ For spaces):
input
❐❐❐❐ Hello ❐❐ everyone, ❐ we ❐ are ❐ xiyouLinuxer ❐
output
xiyouLinuxer ❐ are ❐ we ❐ everyone, ❐ Hello
#include <stdio.h>
#include<string.h>

void reserveworld(char* str,int s,int e) {
	while (s < e) {
		char c = str[s];
		str[s] = str[e];
		str[e] = c;
		s++;
		e--;
	}
}


void reserve(char *str,int n) {
	int t = 0;  
	for (int i = 0; i <= n; i++) {
		if (str[i] == ' ' || str[i] == '\0') {
			reserveworld(str, t, i - 1);
			t = i + 1;
		}
	}
}

int main() {
	char str[100] = "";
	gets(str);
	reserveworld(str, 0, strlen(str)-1);	
	reserve(str, strlen(str));	
	printf("%s", str);

	return 0;	
}

 

 

Posted by Leviathan on Tue, 30 Nov 2021 19:16:54 -0800