当前位置:网站首页>【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;
}
边栏推荐
- Anddroid 文本合成语音TTS实现
- String coordinates of number to excel
- Comprehensive experiment Li
- empirical study and case study
- MATLAB小技巧(23)矩阵分析--模拟退火
- [getting started] intercepting strings
- How to recruit Taobao anchor suitable for your own store
- Koltin35, headline Android interview algorithm
- golang中的正则表达式使用注意事项与技巧
- shardingSphere
猜你喜欢

Intelligent water supply system solution

Redis publish subscription

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation

Using settoolkit to forge sites to steal user information

Airsim radar camera fusion to generate color point cloud

Set up file server Minio for quick use
![[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B](/img/48/e98d01830867baa742574e1b6e1096.jpg)
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B

一套十万级TPS的IM综合消息系统的架构实践与思考

seaborn clustermap矩阵添加颜色块

Hijacking a user's browser with beef
随机推荐
Leetcode t34: find the first and last positions of elements in a sorted array
Soft keyboard height error
Airsim雷达相机融合生成彩色点云
Learn the knowledge you need to know about the communication protocol I2C bus
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
初学者如何正确理解google官方建议架构原则(疑问?)
String coordinates of number to excel
Set up file server Minio for quick use
CPU设计实战-第四章实践任务一简单CPU参考设计调试
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
2022 examination summary of quality controller civil engineering direction post skills (quality controller) and reexamination examination of quality controller civil engineering direction post skills
2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
Provincial election + noi Part VII computational geometry
程序员养生宝典
leetcode T31:下一排列
使用 setoolkit 伪造站点窃取用户信息
The difference between interceptors and filters
Uni hot update
Deep learning systematic learning
使用beef劫持用戶瀏覽器