当前位置:网站首页>【C】 Summary of wrong questions in winter vacation
【C】 Summary of wrong questions in winter vacation
2022-07-01 08:31:00 【Xiao tangxuezha】
choice question
eg1
What is the output of the following code ( )
char a=101;
int sum=200;
a+=27;sum+=a;
printf("%d\n",sum);
A: 327 B: 99 C: 328 D: 72
Analysis of the answer :
right key :D
char Is a signed type , Occupy 1 Bytes , That is to say 8 position , The highest bit is the sign bit , The value range is -128~127;a=101+27=128,128 Expressed as 1000 0000, As a complement in memory , Symbol bit 1, With the int Type of sum The integer will be raised during addition calculation , High compensation 1, And then the original code is -128,sum=200+(-128)=72
eg2
Assume that the function prototype and variables are described below , Then the legal call is ( )
void f(int **p);
int a[4]={
1,2,3,4};
int b[3][4]={
{
1,2,3,4},{
5,6,7,8},{
9,10,11,12}};
int *q[3]={
b[0],b[1],b[2]};
A: f(a); B: f(b); C: f(q); D: f(&a);
Analysis of the answer :
right key :C
A Options ,f(a) Reference time ,a It degenerates into an address that points to its first element , The type is int*, Not in conformity with .B Options ,b Is a two-dimensional array , When a parameter is passed, it degenerates into a pointer to its first element , That is to say b[0] The address of ,b[0] The type is int [4], so &b[0] The type is int()[4], Not in conformity with .D Options ,&a It's an array a The address of , The type is int()[4], Not in conformity with .C Options ,q It's an array of Pointers , Use... During initialization b[0]、b[1]、b[2], here b[0]、b[1]、b[2] It degenerates into a pointer to each first element (int* type , Therefore, the type conforms to , You can use them to initialize ).q Reference time , Degenerate into a pointer to its first element , namely int**, accord with 33
eg3
hypothesis C Used in language programs malloc Applied for memory , But no free fall , So when the process is kill after , The operating system will ( )
A: Memory leak B: segmentation fault C: core dump D: None of the above
Analysis of the answer :
right key :D
No matter how the user program is used malloc, At the end of the process , The memory space created by the user program will be recycled
eg4
What is the output result of the following program ( )
#include <stdio.h>
typedef struct List_t
{
struct List_t* next;
struct List_t* prev;
char data[0];
}list_t;
int main()
{
printf("%d",sizeof(list_t));
return 0;
}
A: 4byte B: 8byte C: 5byte D: 9byte
Analysis of the answer :
right key :B
In the title char data[0] Or written as char data[], Is a flexible array member ; When the size of the computer structure data Not occupy struct Space , It just exists as a symbolic address . therefore sizeof The value of is the byte occupied by two pointers , namely 4+4=8 byte
eg5
The following is about C In language , The wrong is ( )
A: A memory leak usually means that a program has applied for a piece of memory , After use , This memory was not released in time , This causes the program to occupy a large amount of memory , But it's not used or explained
discharge
B: Can pass malloc(size_t) The function call requests a memory block that exceeds the physical memory size of the machine
C: Unable to free function from memory free(void*) Directly return a used memory to the operating system ,free The function will only dynamically request the use of memory
Release of power
D: You can use the memory allocation function malloc(size_t) Directly apply for physical memory
Analysis of the answer :
right key :D
free The memory released is not necessarily returned directly to the operating system , It may not be released until the end of the process .malloc You cannot apply for physical memory directly , It applies for virtual memory
eg6
The output of the following code is ( )
#include <stdio.h>
#define a 10
void foo();
int main()
{
printf("%d..", a);
foo();
printf("%d", a);
return 0;
}
void foo()
{
#undef a
#define a 50
}
A: 10..10 B: 10..50 C: Error D: 0
Analysis of the answer :
right key :A
define In the pre-processing stage, the main Medium a Replace all with 10 了 . in addition , Whether it's in a function , Still outside the function ,define From the definition to the end of the file , So if you take foo The definition of the function is put into main The above words , The result will be 50…50
Programming questions
eg1

Violent solution : Compare each character in the string with all characters ., Whether the breaks are equal .
int FirstNotRepeatingChar(char* str ) {
// write code here
for(int i = 0; i < strlen(str); i++)
{
int flag = 0;// Judgment No. i Whether characters can be found later
for(int j = 0; j < strlen(str)-i; j++)
{
if(j != i && str[i] == str[j])
{
flag = 1;
break;
}
}
if(flag == 0)
{
return i;
}
}
return -1;
}
eg2

Their thinking
Statistics s The number of times each character appears in , if s Can form palindrome string , The maximum number of occurrences of one character is an odd number .
bool canPermutePalindrome(char* s){
char a[256] = {
0};
for(int i = 0; i < strlen(s); i++)
{
a[s[i]]++;
}
int count = 0;
for(int i = 0; i < 128; i++)
{
if(a[i] % 2 != 0)
{
count++;
}
if(count > 1)
{
return false;
}
}
return true;
}
eg3

char* replaceSpaces(char* S, int length0)
{
int count = 0;// Count the number of spaces
int afterLength = 0;// Statistics replace spaces with %20 The length after
for(int i = 0; i < length0; i++)
{
if(S[i] == ' ')
{
count++;
}
}
afterLength = length0 + 2 * count;
S[afterLength] = '\0';// First set the end of string flag
int j = 1;// Record afterLenth The location of
for(int i = length0 - 1; i >=0; i--)
{
if(S[i] != ' ')
{
S[afterLength - j] = S[i];
j++;
}
else
{
S[afterLength - j] = '0';
S[afterLength - j - 1] = '2';
S[afterLength - j - 2] = '%';
j += 3;
}
}
return S;
}
eg4

【 Analysis of the answer 】:
Be careful : This problem is the exchange of odd and even bits of binary , Just move the even bits one bit to the left , Just move the odd digit to the right by one digit
int exchangeBits(int num)
{
int odd = 0b10101010101010101010101010101010;// Keep even digits
int even = 0b01010101010101010101010101010101;// Keep odd digits
odd &= num;
even &= num;
return (odd >> 1) + (even << 1);
}
eg5

1、【 Analysis of the answer 】:
Find the number of factorials of a number mantissa 0 , Here we need to be flexible mantissa 0 How did you get it ? Is times 10 To the , and 10=2*5 , Factorial medium 2 There are many factors , however 5 There is not much , So we just need to count 5 How many times can it appear .
- Violence method : Traverse 5 Multiple , Each judgment 5 How many multiples of 5 , such as 25=5*5 , therefore 25 It needs to be counted twice , This method is direct , But it's not efficient , Because when the number is large, the number of cycles is more …
- Better ideas : With 10 The factorial of 10!=12345678910 , Every time 5 A number will appear once 5 , therefore 10 / 5 Namely 5 Number of occurrences .
But the special thing is 25,125… This includes multiple 5 Of , 25=55, 125=555… , They themselves were counted only once , 25 And then there were 1 Multiple multipliers 5 , 125 And then there were 1 Multiple multipliers 25 , So we need to divide by again 5 , Get the rest 5 The number of , Until the final result is less than 5 until
int trailingZeroes(int n)
{
// Time complexity O(logN)
int count = 0;
while(n >= 5)
{
count += n / 5;
n /= 5;
}
return count;
// Time complexity O(N*logN)
/*int count = 0; for(int i = 0; i <= n; i += 5) { int num = i; while(num > 0 && num % 5 == 0) { count++; num /= 5; } } return count; } eg6

The problem itself is not difficult , With a specific formula, the result can be obtained by direct loop and subsequent derivation . But when there are multiple test cases , Recalculate the... Every time k Data , The efficiency will be much worse , After all, every number has to be recalculated from the beginning .
Therefore, after entering the program, you can first form a larger sequence , In the following test cases, you need to take out the first few directly
#include <stdio.h>
int main()
{
int arr[1000000] = {
1, 2};
for(int i = 2; i < 1000000; i++)
{
arr[i] = (2 * arr[i - 1] + arr[i - 2]) % 32767;
}
int n = 0;
scanf("%d", &n);
int k = 0;
while(n)
{
scanf("%d", &k);
printf("%d\n", arr[k - 1]);
n--;
}
return 0;
}
边栏推荐
- [deep analysis of C language] - data storage in memory
- Provincial election + noi Part VII computational geometry
- Leetcode t29: divide two numbers
- 如何招到适合自己店铺的淘宝主播
- Access report realizes subtotal function
- 2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
- Redis publish subscription
- 使用 setoolkit 伪造站点窃取用户信息
- Leetcode t34: find the first and last positions of elements in a sorted array
- Learn the knowledge you need to know about the communication protocol I2C bus
猜你喜欢
![[untitled]](/img/b9/6922875009c2d29224a26ed2a22b01.jpg)
[untitled]

2022.2.15

seaborn clustermap矩阵添加颜色块

Use threejs simple Web3D effect
![[JS reverse] MD5 encryption parameter cracking](/img/06/0610045d287f646479d6eb5021a067.png)
[JS reverse] MD5 encryption parameter cracking

SPL installation and basic use (II)

Burpsuite -- brute force cracking of intruder

Pipeline detection of UAV Based on gazebo

The data analyst will be ruined without project experience. These 8 project resources will not be taken away

SPL-安装与基本使用(二)
随机推荐
Download jackson codehaus. org jar - downloading jackson. codehaus. org jar
01 NumPy介绍
Intelligent water supply system solution
谈谈数字化转型的几个关键问题
EDA open source simulation tool verilator beginner 6: debugging examples
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
[detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]
[untitled]
[getting started] intercepting strings
Yolov5 advanced six target tracking environment construction
The data analyst will be ruined without project experience. These 8 project resources will not be taken away
Chinese font Gan: zi2zi
Yolov5 advanced 7 target tracking latest environment setup
栈实现计算器
Leetcode t31: prochain arrangement
[untitled]
MAVROS发送自定义话题消息给PX4
Agrometeorological environment monitoring system
【js逆向】md5加密参数破解
Leetcode t31: next spread