当前位置:网站首页>"C language" question set of ⑩
"C language" question set of ⑩
2022-06-26 16:04:00 【Chenze】
write in front
Hello everyone , I'm Chen Ze , I hope after you read it , It can help you , Insufficient, please correct ! Learn and communicate together
2021 Blog star of the year, Internet of things and embedded development TOP5~2021 Blog star Top100~ Alibaba cloud experts ^ Star Blogger ~ Nuggets ⇿InfoQ Creator ~ Zhou Bang 77» General list 1766
This paper is written by Chen Ze original CSDN First episode If you need to reprint, please inform
Personal home page - Chen Ze's blog _CSDN Blog
Welcome to → give the thumbs-up + Collection ️ + Leaving a message.
Series column -【C】 subject _ Chen Ze's blog -CSDN Blog
️ We are not on the stage of our choice , Acting is not the script we chose

『C Language 』 Problem set of ⑩ Directory as follows ⇲
Question 46 → Create a custom function , So as to achieve strcat() The function of
Question 47 → seek 1! + 2! + 3! ... +n!; Don't think about spillovers
Question 48 → Create a custom function , Implement string functions strcpy()
Question 49 → Calculated at n How many binary complements are there in the parameters of 1
Question 50 → Design an algorithm , Find input A and B The least common multiple of

Question 46 → Create a custom function , So as to achieve strcat() The function of
First of all, when realizing this topic , We need to know first strcat() A basic information of function .
Let's see first strcat() The function declaration of the function is as follows ↓
char *strcat(char *dest, const char *src)Append a copy of the source string to the destination string .
dest → Point to target array , The array contains a C character string , And enough to hold the appended string .
src → Point to the string to append , The string does not override the target string .
This function returns a string pointing to the final destination dest The pointer to .
hold src The string pointed to is appended to dest The end of the string that you are pointing to .
That's all strcat() A basic information of function , I believe that when we know this, we can solve this problem well *⌒(*^-゜)v THX!!
Question 47 → seek 1! + 2! + 3! ... +n!; Don't think about spillovers
This can actually be used for The loop can be done . This topic is actually an easy one , But the comparison needs to examine the ability of logical thinking .
1! + 2! + 3! ... +n! The assumption is 4 that →1+1×2+1×2×3+1×2×3×4.
remember : The problem is that there is no need to consider whether the program results in stack overflow .
In fact, you can refer to Substitution method A way to solve such problems would be much better .
Question 48 → Create a custom function , Implement string functions strcpy()
Do this to create a string function to achieve its functions , First, we must understand the function of the custom function we created , This is conducive to our better realization .
strcpy() The function is declared as follows ↓
char *strcpy(char *dest, const char *src)hold src The string you point to is copied to dest.
Note that if the target array dest Not big enough , And the length of the source string is too long , It may cause buffer overflow . therefore ,dest Be big enough , So that we can be src Put it away .
dest→ Point to the target array used to store the copied content .
src→ The string to copy .
This function returns a string pointing to the final destination dest The pointer to .
strcpy() In the original character to ensure that you have src The space size of existing characters is also called subscript .
Be careful : The pointer type of the return value here can be void It can also be char *
Question 49 → Calculated at n How many binary complements are there in the parameters of 1
The subject : Compare and examine your logical thinking ability, which is actually a use of operators .( The key to solving the problem is )
Calculated at n How many binary complements are there in the parameters of 1, That is, when we input numbers when we input them , It can calculate the number of complements among us 1. Let's take a number of input integer types as an example , In doing this problem, we must know the original code 、 Inverse code 、 What exactly is complement (^∀^●)ノシ
Original code → Directly convert the number into binary according to the positive or negative form .
Inverse code → Leave your sign bit unchanged , That is, the highest position remains unchanged , Secondly, reverse the order of bits , Get the inverse .
Complement code → The complement is Inverse code +1 You can get the complement , Be careful : The premise is to reverse the code , On the basis of +1.
Question 50 → Design an algorithm , Find input A and B The least common multiple of
I entered two numbers in the previous topic , Find their greatest common divisor . So in this exercise, we will design an algorithm , Find input A and B The least common multiple of . Before that, we need to know what is the least common multiple , In this way, we can have a train of thought for solving problems (@^0^)
The multiples of two or more integers are called their common multiples , In addition to 0 The smallest common multiple outside is called the smallest common multiple of these integers . Integers a,b The least common multiple of is [a,b], alike ,a,b,c The least common multiple of is [a,b,c], The least common multiple of multiple integers has the same notation .
The concept corresponding to the least common multiple is the greatest common divisor ,a,b The greatest common divisor of is (a,b). On the least common multiple and the greatest common divisor , We have this theorem :(a,b)x[a,b]=ab(a,b All are integers ).
Let's use a diagram to show ↓
Question 46 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char *My_strcat(char *dest, const char *src)
{
assert(dest && src != NULL);// Assertion
char *ret = dest;
while (*dest != '\0')//'\0' Of ASCLL The code value is 0
{
dest++;
}
//dest Pointing to '\0'
while (*dest++ = *src++)
{
;
}
/* amount to
while (*src != '\0')
{
*dest++ = *src++;
}*/
return ret;
}
int main(void)
{
char arr1[20] = "hello C";
char arr2[20] = "yuyan";
printf("%s\n", My_strcat(arr1, arr2));
return 0;
}The operation results are as follows ↓
hello Cyuyan
Question 47 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main(void)
{
// Substitution method 1*1 + 1*2 + 1*2*3 + 1*2*3*4 - Suppose you enter a number :4
int i = 0;
int j = 0;
int num = 0;
int sum = 0;
printf(" Please enter a number ->:");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
int ret = 1;// Be careful ->ret
for (j = 1; j <= i; j++)
{
ret = j * ret;// The sum of each class
}
sum = ret + sum;// The sum of the
}
printf("sum = %d\n", sum);
return 0;
}The operation results are as follows ↓
Please enter a number ->:4
sum = 33
Question 48 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
void my_strcpy(char* str1, char* str2)
{
assert(str1 && str2 != NULL);// Assertion !
// Put the string str2 Assign a value to str1, encounter '\0' end .
while (*str2 != '\0')
{
*str1++ = *str2++;
}
}
int main(void)
{
char str[20] = { 0 };
char p[20] = { 0 };
printf(" Please enter the string ->:");
scanf("%s", str);
my_strcpy(p, str);
printf("ret = %s\n",p);
return 0;
}Running results
Please enter the string ->:C Language yyds!
ptr = C Language yyds!
Question 49 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int function(int n)
{
int count = 0;
int i = 0;
for (i = 0; i < 32; i++)
{
// hypothesis n = 3
// 0011 >> 0 - 0011 & 1111 +1
// 0011 >> 1 - 0001 & 1111 +2
// 0001 >> 2 - 0000 & 1111 count = 2
if (((n >> i) & 1) == 1)
{
count++;
}
}
return count;
}
int main(void)
{
int n = 0;
printf(" Please enter a number :");
scanf("%d", &n);
int ret = function(n);
printf("ret = %d\n", ret);
return 0;
}Running results
hypothesis → Please enter a number :3
ret = 2
Question 50 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
typedef unsigned long int u_lint;
int main(void)
{
int i = 1;
u_lint a = 0;
u_lint b = 0;
printf(" Please enter two numbers ->:");
scanf("%d %d", &a, &b);
while (i)
{
if (a*i % b == 0)
{
printf(" Minimum common multiple :%d\n", a*i);
break;
}
i++;// Be careful →i++ The location of
}
return 0;
}Running results
Possible input results → Please enter two numbers ->:90 6
Minimum common multiple :90
边栏推荐
- IntelliJ idea -- Method for formatting SQL files
- 零知识 QAP 问题的转化
- [time complexity and space complexity]
- 还存在过有键盘的kindle?
- (一)keras手写数字体识别并识别自己写的数字
- [untitled]
- Canvas three dot flashing animation
- How to create your own NFT (polygon) on opensea
- 【leetcode】112. Path sum - 113 Path sum II
- Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
猜你喜欢

Svg animation around the earth JS special effects

IntelliJ idea -- Method for formatting SQL files

STEPN 新手入門及進階

Everyone is a scientist free gas experience Mint love crash

Svg savage animation code

NFT Platform Security Guide (1)

Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT

如何辨别合约问题

El dialog drag and drop, the boundary problem is completely corrected, and the bug of the online version is fixed

5000 word analysis: the way of container security attack and defense in actual combat scenarios
随机推荐
『C语言』题集 of ⑩
振动式液量检测装置
js创意图标导航菜单切换背景色
C language reading data
STEPN 新手入门及进阶
NFT Platform Security Guide (1)
SVG大写字母A动画js特效
Nanopi duo2 connection WiFi
【leetcode】112. Path sum - 113 Path sum II
JS text scrolling scattered animation JS special effect
6 custom layer
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
Reflection modification final
基于 MATLAB的自然过渡配音处理方案探究
JS creative icon navigation menu switch background color
Development, deployment and online process of NFT project (1)
为什么图像分割任务中经常用到编码器和解码器结构?
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
NFT 平台安全指南(2)

