当前位置:网站首页>【C 题集】of Ⅴ
【C 题集】of Ⅴ
2022-07-02 21:48:00 【InfoQ】
write in front
第二十一题→假设输入数字5,实现1+2+3+4+5=16,同样输入任何数字产生这样每个数字相加之和(递归方法实现)
- 每一个递归函数都应该只进行有限次的递归调用,否则它就会进入死胡同,永远也不能退出了,这样的程序是没有意义的。
- 存在限制条件,当满足这个限制条件之后的时候,递归便会不再继续。
- 每次递归调用之后都会越来越接近这个限制条件。
第二十二题→用指针实现两个值的交换,不能创建临时变量来进行交换替换
第二十三题→编写代码,演示多个字符从两端移动,向中间汇聚。(循环一次延迟1s再然后清屏,最后打印出字符)
char arr1[] = "Cyuyuanyyds";
char arr2[] = "###########";
Sleep();//延时函数,注意:S要大写
system("cls");//清屏函数
第二十四题→用 switch 分支语句输入星期一到星期天,如果输入不是星期一到星期天范围就打印 eroor (switch语句实现)
switch(表达式)
{
case 常量表达式 1:
语句 1;
case 常量表达式 2:
语句 2;
…
case 常量表达式 n:
语句 n;
default :
默认情况语句块;
}
第二十五题→请输入密码,然后输入 Y 是确认密码,N 是确认失败
第二十一题代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int face(int n)
{
if (n <= 1)
return 1;
else
return (n+face(n - 1));//注意优先级
}
int main(void)
{
int n;
printf("请输入你的数字:");
scanf("%d", &n);
int ret = face(n);
printf("N = %d\n", ret);
return 0;
}
第二十二题代码
#include<stdio.h>
void swap(int *x, int *y)
{
//按位异或 假设 是 x = 1,y = 2
*x ^= *y;//x = x^y // 0001 ^ 0010 = 0011 = 3 此时x = 3
*y ^= *x;//y = x^y // 0011 ^ 0010 = 0001 = 1 此时y = 1
*x ^= *y;//x = x^y // 0011 ^ 0001 = 0010 = 2 此时x = 2 结果 x = 2,y = 1
}
int main(void)
{
int a, b;
printf("请输入两个数字:");
scanf("%d %d", &a, &b);
printf("a,b交换前: a = %d,b = %d\n", a, b);
swap(&a, &b);
printf("a,b交换后: a = %d,b = %d\n", a, b);
return 0;
}
第二十三题代码
#include<stdio.h>
#include<string.h>//strlen长度的头文件
#include<windows.h>//system("");执行命令头文件
int main(void)
{
char arr1[] = "nageshijieshimeng";
char arr2[] = "#################";
int left = 0;//下标 访问下标元素从0开始
int right = strlen(arr1) - 1;//长度字符串 下标 下标比元素个数少1,所以需要减1
while (left <= right)//当下标<=下标(字符)时候则停止循环
{
arr2[left] = arr1[left]; //把[arr1]n=[arr2]# 左边left第一个字符
arr2[right] = arr1[right];//把[arr2]g=[arr2]# 右边right第一个字符
printf("%s\n", arr2);
Sleep(1000); //延迟1000ms = 1s
system("cls");//清空当前屏幕
left++;
right--;
}
printf("%s\n", arr2);//最后打印出结果
return 0;
}
第二十四题代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
int input = 0;
printf("Please enter:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("eroor\n");
}
}
第二十五题代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
char password[20] = { 0 };
int ret = 0;
printf("请输入密码:");
//password是数组名,数组名本身就已经取地址了。
scanf("%s", password);
//当我们程序scanf函数在读取缓冲区时候有\n,所以我们应该把\n的这个字符给读取了!注意缓冲区会保留\n
int ch;
while ((ch = getchar()) != '\n');//搭建while循环直到把缓冲区的'\n'读取完毕
printf("请确认密码(Y/N):");
ret = getchar();//读取完毕之后,再进行getchar()的一个输入
if (ret == 'Y')
{
printf("确认成功!\n");
}
if (ret == 'N')
{
printf("确认失败!\n");
}
if (ret != 'Y'&&'N')
{
printf("宁输入的格式有误!\n");
}
return 0;
}
边栏推荐
- *C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
- 分享一下如何制作专业的手绘电子地图
- [shutter] shutter page Jump (route | navigator | page close)
- Construction and maintenance of business websites [10]
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- From personal heroes to versatile developers, the era of programmer 3.0 is coming
- China's Micro SD market trend report, technology dynamic innovation and market forecast
- 【零基础一】Navicat下载链接
- Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
- 技术人创业:失败不是成功,但反思是
猜你喜欢

Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision

kubernetes资源对象介绍及常用命令(四)

TinyMCE visual editor adds Baidu map plug-in

图像基础概念与YUV/RGB深入理解

Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme

Three chess games
![[shutter] shutter layout component (physicalmodel component)](/img/6a/f8161fb7c8e9012456622f1920da64.gif)
[shutter] shutter layout component (physicalmodel component)

Infrastructure is code: a change is coming

The neo4j skill tree was officially released to help you easily master the neo4j map database

【零基础一】Navicat下载链接
随机推荐
暑期第一周总结
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
[Jianzhi offer] 57 And are two numbers of S
20220702-程序员如何构建知识体系?
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
[shutter] statefulwidget component (pageview component)
Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
~90z axis translation
Redis distributed lock failure, I can't help but want to burst
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
记录一下微信、QQ、微博分享web网页功能
攻防世界pwn题:Recho
MySQL inserts Chinese data and reports an error. Set the default collation
Pyqt picture decodes and encodes and loads pictures
Jar package startup failed -mysql modify the default port number / set password free enter
2019 Nanchang (relive the classic)
MySQL learning record (1)
MySQL learning record (2)