当前位置:网站首页>"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
边栏推荐
- 【leetcode】701. 二叉搜索树中的插入操作
- Cookie和Session详解
- Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
- Binding method of multiple sub control signal slots under QT
- C# 读写文件从用户态切到内核态,到底是个什么流程?
- IAR工程适配GD32芯片
- STEPN 新手入門及進階
- (DFS search) acwing 2005 horseshoe
- JVM笔记
- 今年高考英语AI得分134,复旦武大校友这项研究有点意思
猜你喜欢

Development, deployment and online process of NFT project (2)

js创意图标导航菜单切换背景色

Tweenmax+svg switch color animation scene

svg野人动画代码

OpenSea上如何创建自己的NFT(Polygon)

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

JS text scrolling scattered animation JS special effect

Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统

Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law

NFT Platform Security Guide (1)
随机推荐
8 user defined evaluation function
「干货」NFT 上中下游产业链全景分析
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
6 自定义层
人人都当科学家之免Gas体验mint爱死机
Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
Big talk Domain Driven Design -- presentation layer and others
What is the process of switching C # read / write files from user mode to kernel mode?
Tweenmax+svg switch color animation scene
Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
R语言plotly可视化:plotly可视化归一化的直方图(historgram)并在直方图中添加密度曲线kde、并在直方图的底部边缘使用geom_rug函数添加边缘轴须图
Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
7 自定义损失函数
[time complexity and space complexity]
Practice of federal learning in Tencent micro vision advertising
5000字解析:实战化场景下的容器安全攻防之道
11 cnn简介
IntelliJ idea -- Method for formatting SQL files
svg上升的彩色气泡动画
HW safety response

