当前位置:网站首页>C language question brushing series (III)
C language question brushing series (III)
2022-06-10 03:33:00 【Small process】
This series is just a summary of my own study , Please give me some praise !
Catalog
1. Calculation n The factorial
2. Calculation 1!+2!+3!+...+10!
3. Find a specific number in an ordered array .
1. Calculation n The factorial
Ideas :
To count n The factorial , First we have to have a number n,n The factorial of is from 1*2*3...*n Of , So you also need to have a loop variable i To multiply all the time .
int main()
{
int n = 0;
scanf("%d", &n);
int i = 0;
int ret = 1;
for (i = 1; i <= n; i++)
{
ret = ret * i;
}
printf("%d\n", ret);
return 0;
}2. Calculation 1!+2!+3!+...+10!
Ideas :
First , We are based on the code in the previous figure , We can draw n The factorial , That request 1!+2!+3!+...+10! Just add the factorials of each number together , Use one sum Save the variable .
int main()
{
int n = 0;
scanf("%d", &n);
int i = 0;
int ret = 1;
int sum = 0;
for (i = 1; i <= n; i++)
{
ret = ret * i;
sum = sum + ret;
}
printf("%d\n", sum);
return 0;
}3. Find a specific number in an ordered array .
Ideas :
( Two points search ) Here I will directly use pictures to explain the process of binary search 



The words are ugly , I hope you can bear more .
This is also the code for binary search
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 12;
int sz = sizeof(arr) / sizeof(arr[0]);
// Here is a way to find the subscript at the end of an array
int left = 0;
int right = sz - 1;
//int mid = (left + right) / 2;
/*int mid = left + (right - left) / 2;*/
// It is better to write here in the following way .
while (left<=right)
{
//int mid = (left + right) / 2;
int mid = left + (right - left) / 2;
// It is better to write here in the following way .
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
printf(" eureka , The subscript is :%d\n", mid);
break;
}
}
if (left > right)
{
printf(" Can not find ");
}
return 0;
}There are a few things to pay attention to ,
int mid = (left + right) / 2;
int mid = left + (right - left) / 2;
It is better to write here in the following way ., Because the size of the shape type has a range , In case your two numbers add up out of range , It will lead to mistakes .
int sz = sizeof(arr) / sizeof(arr[0]);
Here is a way to find the subscript at the end of an array
4. Write code , Demonstrate the movement of multiple characters from both ends , Converging in the middle .
Ideas :
welcome to anhui!!!!!!
######################
w####################!
we##################!!
wel################!!!
...
welcome to anhui!!!!!!
First , It doesn't matter , We need to have two arrays , Then, you can overlay the elements of the above array on the lower array , Another cycle .
#include <string.h>
#include <windows.h>
#include <stdlib.h>
//
int main()
{
char arr1[] = "welcome to anhui!!!!!!";
char arr2[] = "######################";
int left = 0;
int right = strlen(arr1) - 1;
while (left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
left++;
right--;
Sleep(1000);
system("cls");
}
printf("%s\n", arr2);
return 0;
}Pay attention here , Two library functions ,Sleep Is contained in stdlib In this header file , In milliseconds .system yes windows Self contained , and clc Is the command to clear the screen .
5. Write code to achieve , Simulate user login scenarios , And can only log in three times .( Only three passwords are allowed , If the password is correct, the login is successful , If all three inputs are wrong , Then exit the program .)
Ideas :
You can only log in three times , Then use a variable to represent three times , Then compare whether the input string and password are equal .
It is important to note that the comparison of two strings cannot be used ==
You should use strcmp
int main()
{
int n = 0;
char password[20] = { 0 };
// Suppose the password is abcdef
for (n = 1; n <= 3; n++)
{
printf(" Please input a password :");
scanf("%s", password);
if (strcmp(password, "abcdef") == 0)
{
printf(" The password is correct \n");
break;
}
else
{
printf(" Reenter the password \n");
}
}
if (4 == n)
{
printf(" Input error , Exit procedure \n");
}
return 0;
}边栏推荐
猜你喜欢

答辩前电脑坏了......

Refactoring technique --extract class

报错信息:不兼容的类型。实际为 某某某,需要‘com.alibaba.excel.enums.poi.HorizontalAlignmentEnum‘(EasyExcel内容样式代码报错)

Wang Xing, Zhang Yong, Xu Lei, who can win the local e-commerce?

Is long-term hotel rental a good business?

vulnhub之hacksudo:Thor

【pytorch 修改预训练模型:实测加载预训练模型与模型随机初始化差别不大】

三個月GMV6000w+,盤點家紡行業打造爆款的關鍵

ACL 2022 | the latest hot research in NLP field, you must not miss it!

【PyTorch预训练模型修改、增删特定层】
随机推荐
Broadcast has increased by 5000W, and "playing emotion cards" has become a new trend of the platform
The new account started to attract nearly 2million fans, and the "old age" account can also become a dark horse for promotion
Storage of signed and unsigned shaping in memory
重构--代码坏味道
指针的进阶(C语言超详细指针介绍)
Refactoring method --extract method
Huawei Hubble will add another IPO, and Maxon will rush to the scientific innovation board after more than ten years of dormancy
张量(tensor)编程
From ancient literature to cloud technology
vulnhub之HARRYPOTTER: FAWKES
The playback capacity of a single video is more than 8000w, so hard core cooking is so superior
【pytorch 修改预训练模型:实测加载预训练模型与模型随机初始化差别不大】
Implementation of multithreading
基于用户行为的生物探针和智能验证码
新功能官宣丨PlayFab让跨平台游戏架构如此简单
决策引擎系统 && 实时指标计算 && 风险态势感知系统 && 风险数据名单体系 && 欺诈情报体系
【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
PtrToStructure 错误提示:此结构不得为值类,解决办法
【yolov5.yaml解析】
497. random points in non overlapping rectangles