当前位置:网站首页>【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;
}
边栏推荐
- [dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
- P4 installation bmv2 detailed tutorial
- SPL-介绍(一)
- Leetcode T39: 组合总和
- Tita OKR: a dashboard to master the big picture
- There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
- Precautions and skills in using regular expressions in golang
- Set up file server Minio for quick use
- Anddroid text to speech TTS implementation
- [untitled]
猜你喜欢
![[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order](/img/87/07783593dbabcf29700fa207ecda08.png)
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order

2022.2.15

CPU设计实战-第四章实践任务一简单CPU参考设计调试

Redis publish subscription

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

OJ输入输出练习

Instead of houses, another kind of capital in China is rising

使用beef劫持用户浏览器

Adding color blocks to Seaborn clustermap matrix

【无标题】
随机推荐
getInputStream() has already been called for this request
Intelligent water supply system solution
程序员养生宝典
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
golang中的正则表达式使用注意事项与技巧
Access report realizes subtotal function
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
[force deduction 10 days SQL introduction] Day9 control flow
Why are some Wills made by husband and wife invalid
Mavros sends a custom topic message to Px4
Leetcode t34: find the first and last positions of elements in a sorted array
Intelligent water and fertilizer integrated control system
Adding color blocks to Seaborn clustermap matrix
Huawei machine test questions column subscription Guide
XX attack - reflective XSS attack hijacking user browser
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
Comprehensive experiment Li
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)
Cmake I two ways to compile source files
Conception et mise en service du processeur - chapitre 4 tâches pratiques