当前位置:网站首页>[dish of learning notes dog learning C] exercise
[dish of learning notes dog learning C] exercise
2022-07-21 03:14:00 【Jiang Junzhu】
1.n The factorial
int main() {
int n = 0;
int x = 1;
printf(" Please enter a number :");
scanf("%d", &n);
int i = 0;
for (i = 1; i <= n; i++) {
x *= i;
}
printf("n The factorial is :%d", x);
return 0;
}
2. Calculation 1!+2!+3!+……+10!
int main() {
int i = 0, j = 0;
int ret = 1;
int sum = 0;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= i; j++) {
ret *= j;
}
sum += ret;
ret = 1;
}
printf("1!+2!+3!+……+10! by :%d", sum);
return 0;
}
Calculation 1!+2!+3!+……+10! Code optimization
int main() {
int i = 0, j = 0;
int ret = 1;
int sum = 0;
for (i = 1; i <= 10; i++) {
ret *= i;
sum += ret;
}
printf("1!+2!+3!+……+10! by :%d", sum);
return 0;
}
- Find a specific number in an ordered array N, To write int binsearch(int x, intv[]); function : stay v[0]<=v[1]<=v[2]<=……<=v[n-1] Find in the array of X
int main() {
int arr[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
int k = 7;// The number to find
// stay arr Find in this ordered array k(7) Value
int sz = sizeof(arr) / sizeof(arr[0]);// The number of elements in the array
int left = 0;
int right = sz - 1;
while (left <= right) {
int mid = (left + right) / 2;
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't find \n");
}
return 0;
}
4. Write code , Delay multiple characters to move from both ends , Converging in the middle
int main() {
char arr1[] = {
"I want to reason with the world!" };
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);
Sleep(1000);// Sleep for a second
system("cls");// Clear the screen
left++;
right--;
}
printf("%s\n", arr2);
return 0;
}
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 .
int main() {
int i = 0;
// Assume the correct password is 123456
char password[20] = {
0 };
for (i = 0; i < 3; i++) {
printf(" Please input a password :");
scanf("%s", password);
// Comparison of two strings , Out of commission ==, You should use strcmp
if (strcmp(password, "123456") == 0){
printf(" Login successful \n");
break;
}
else {
printf(" Wrong password , Please re-enter !\n");
}
}
if (i == 3) {
printf(" Three wrong passwords , Exit procedure \n");
}
return 0;
}
边栏推荐
- [dish of learning notes, dog learning C] getting to know the pointer for the first time
- oralce mapping 映射CLOb
- 一文了解 NebulaGraph 上的 Spark 项目
- ORALCE mapping CLOB
- Apache Doris Oracle ODBC appearance User Guide
- 记笔记之Hal库串口
- B. All Distinct
- 【学习笔记之菜Dog学C】练习
- Graduation season -- use technology of various application scenarios
- Automatic saving function in LabVIEW
猜你喜欢
Apache Doris Binlog Load使用方法及示例
easyExcel设置最后一行的样式【可以拓展为每一行】
Visual Studio 开发环境的配置
【学习笔记之菜Dog学C】数组
Binary tree implementation (generate binary tree according to hierarchical array)
Understand and apply continuous deployment Argo CD
DOM -- event syntax
DOM——事件语法
文件编辑器vim的使用和介绍
Ctfshow web entry information collection WP (1-20) (detailed explanation)
随机推荐
Oracle implements the limit effect: the use of rownum
2022-7-19 第八小组 顾宇佳 学习笔记 (this关键字和封装)
[note] logstash environment setup and installation configuration
【学习笔记之菜Dog学C】指针进阶
[sort] bucket sort and cardinal sort
How to realize the secondary drop-down of select in wechat applet
Flink Doris Connector设计方案
Excuse me, teachers, how to choose the startup mode in the flynk SQL Kafka connector
Automatic saving function in LabVIEW
LabVIEW中的自动保存功能
【学习笔记之菜Dog学C】初识操作符和原码、反码、补码
Apache Doris ODBC appearance PostgreSQL User Guide
【学习笔记之菜Dog学C】数据存储
B. Almost Ternary Matrix
(洛谷)P1605迷宫(一入深搜苦如海,从此时间是大die)
Usage and examples of Apache Doris binlog load
将流转化为数据产品
多分类的风险评分数据实操,助您中小微业务稳人一步
easyExcel设置最后一行的样式【可以拓展为每一行】
P7354 「PMOI-1」骑士の棋