当前位置:网站首页>C语言经典一百题(1-10题)(含答案)
C语言经典一百题(1-10题)(含答案)
2022-07-21 07:30:00 【Hush_H】
目录
1、求1!+2!+3!+...+20!的和。
2、 利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来
3、 输入一个小于5位的正整数,分别输出它的位数及其逆序打印出各位数字
4、 请输入星期几的第一个字母来判断是星期几,如果第一个字母一样,则继续判断第二个字母
5、 函数调用的用法实例
6、求 2/1,3/2,5/3,8/5,13/8,21/13...这个数列的前20项之和。
7、 打印出所有的"水仙花数(一个三位数,其各位数字立方和等于该数本身)"
8、 输出 101 到 200 之间的素数。
9、 输出9*9乘法表口诀
10、输入三个整数x,y,z,请把这三个数由小到大输出。
1、求1!+2!+3!+...+20!的和。
#include <stdio.h>
int main()
{
int i;
long double sum = 0, mix = 1;
for (i = 1; i <= 20; i++)
{
mix = mix * i;
sum = sum + mix;
}
printf("%Lf\n", sum);
return 0;
}
2、 利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来
#include <stdio.h>
void hello(n)
{
char ch;
if (n <= 1) {
ch = getchar();
printf("相反顺序输出结果:");
putchar(ch);
}
else {
ch = getchar();
hello(n - 1);
putchar(ch);
}
}
int main()
{
int n = 0;
int i = 5;
printf("请输入5个字符:");
hello(i);
printf("\n");
return 0;
}
3、 输入一个小于5位的正整数,分别输出它的位数及其逆序打印出各位数字
#include <stdio.h>
int main()
{
int a, b, c, d, e, x;
printf("请输入小于5位的数字:");
scanf("%d", &x);
a = x / 10000; /*万位*/
b = x % 10000 / 1000; /*千位*/
c = x % 1000 / 100; /*百位*/
d = x % 100 / 10; /*十位*/
e = x % 10; /*个位*/
if (a != 0) {
printf("为 5 位数,逆序为: %d %d %d %d %d\n", e, d, c, b, a);
}
else if (b != 0) {
printf("为 4 位数,逆序为: %d %d %d %d\n", e, d, c, b);
}
else if (c != 0) {
printf("为 3 位数,逆序为:%d %d %d\n", e, d, c);
}
else if (d != 0) {
printf("为 2 位数,逆序为: %d %d\n", e, d);
}
else if (e != 0) {
printf("为 1 位数,逆序为:%d\n", e);
}
return 0;
}
4、 请输入星期几的第一个字母来判断是星期几,如果第一个字母一样,则继续判断第二个字母
#include <stdio.h>
int main()
{
char i, j;
printf("请输入第一个字母:");
scanf("%c", &i);
getchar();//处理‘\n’
switch (i)
{
case 'm':
printf("monday\n");
break;
case 'w':
printf("wednesday\n");
break;
case 'f':
printf("friday\n");
break;
case 't':
printf("请输入下一个字母\n");
scanf("%c", &j);
if ('u' == j) {
printf("tuesday\n");
break;
}
if ('h' == j) {
printf("thursday\n");
break;
}
case 's':
printf("请输入下一个字母\n");
scanf("%c", &j);
if ('a' == j) {
printf("saturday\n");
break;
}
if ('u' == j) {
printf("sunday\n");
break;
}
default:
printf("error\n");
break;
}
return 0;
}
5、 函数调用的用法实例
#include <stdio.h>
void hello(int n) {
if (n) {
printf("Hello World!\n");
hello(n - 1);
}
}
int main() {
int n;
printf("打印多少次:");
scanf("%d", &n);
hello(n);
return 0;
}
6、求 2/1,3/2,5/3,8/5,13/8,21/13...这个数列的前20项之和。
#include <stdio.h>
int main()
{
int i, t;
float sum = 0;
float a = 2, b = 1;
for (i = 1; i <= 20; i++)
{
sum = sum + a / b;
t = a;
a = a + b;
b = t;
}
printf("%f\n", sum);
return 0;
}
7、 打印出所有的"水仙花数(一个三位数,其各位数字立方和等于该数本身)"
int main()
{
int i,x,y,z;
for(i=100;i<1000;i++)
{
x=i%10;
y=i/10%10;
z=i/100%10;
if(i==(x*x*x+y*y*y+z*z*z))
printf("%d\n",i);
}
return 0;
}
8、 输出 101 到 200 之间的素数。
#include <stdio.h>
int main()
{
int i, j;
int count = 0;
for (i = 101; i <= 200; i++)
{
for (j = 2; j < i; j++)
{
// 如果 j 能被 i 整除再跳出循环
if (i % j == 0)
break;
}
// 判断循环是否提前跳出,如果 j<i 说明在 2~j 之间,i 有可整除的数
if (j >= i)
{
count++;
printf("%d ", i);
// 换行,用 count 计数,每五个数换行
if (count % 5 == 0)
printf("\n");
}
}
return 0;
}
9、 输出9*9乘法表口诀
#include <stdio.h>
int main()
{
int i, j, result;
for (i = 1; i < 10; i++)
{
for (j = 1; j <= i; j++)
{
result = i * j;
printf("%d*%d=%-3d", i, j, result); /*-3d表示左对齐,占3位*/
}
printf("\n"); /*每一行后换行*/
}
return 0;
}
10、输入三个整数x,y,z,请把这三个数由小到大输出。
#include <stdio.h>
int main()
{
int x, y, z, t;
printf("请输入三个数字:\n");
scanf("%d %d %d", &x, &y, &z);
if (x > y) { /*交换x,y的值*/
t = x;
x = y;
y = t;
}
if (x > z) { /*交换x,z的值*/
t = z;
z = x;
x = t;
}
if (y > z) { /*交换z,y的值*/
t = y;
y = z;
z = t;
}
printf("从小到大排序: %d %d %d\n", x, y, z);
return 0;
}
边栏推荐
- How to understand pointers?
- Mysql07 (data update DML)
- What is the difference between int *const p= & I and int const *p= & I
- 【集训DAY6】Game【数学】
- 【学习笔记】AGC008
- 使用CompletableFuture实现异步回调
- mysql数据恢复
- Word 2016 strange problem: file save error copy crash
- Using completable future to implement asynchronous callback
- I.MX6U-ALPHA开发板(按键输入实验)
猜你喜欢
Malloc and space Configurator
[terminal _1]-xshell 5 the hottest terminal software!
音频自动增益控制 AGC 解决的问题及原理解析
FreeRTOS -- a method to detect the usage of task stack
Selenium被检测为爬虫,怎么屏蔽和绕过
应用层——典型协议解析
铜牛机房项目的优势和劣势
[Yugong series] go teaching course in July 2022 014 arithmetic operators of operators
UnityWebGl项目总结(未完)
氟尼辛肽核酸寡聚体复合物|规活性基团Alkyne炔烃,SH Thiol炔基修饰肽核酸
随机推荐
44: Chapter 4: develop file service: 5: integrate fastdfs in [files] file service to realize the logic of [upload avatar]; (including: integrating fastdfs in the project; cross domain issues; creating
学会自动化必备工具-Selenium-再想着入坑自动化测试吧
叠氮化物标记pna肽核酸|亚甲基蓝标记pna肽核酸|酪氨酸修饰肽核酸PNA|Tyr-PNA齐岳bio
【sciter】:窗口通信
Mysql05 (view)
Mysql05(视图)
【集训DAY10】Linear【数学】【思维】
Can multithreading optimize program performance?
【集训DAY8】Tent【数学】【DP】
(pytorch advanced road VII) attention based seq2seq
铜牛机房项目的优势和劣势
【MSP430G2553】图形化开发笔记(1) 配置环境
malloc 和 空间配置器
Kaptcha验证码的配置与使用
怎么学自动化测试,可以自学吗?
Easyexcel is easy to use
在go语言中获取当前时间,以及md5、hmac、sha1算法的简单实现
mysql docker安装 主从部署
[terminal _1]-xshell 5 the hottest terminal software!
PYQT5打包出错,缺少文件,例ImportError: OpenCV loader: missing configuration file: [‘config.py‘]. Check