C语言经典例题-18

1.判断是不是字母

题目描述:

KK想判断输入的字符是不是字母,请帮他编程实现。

输入描述:

多组输入,每一行输入一个字符。

输出描述:

针对每组输入,输出单独占一行,判断输入字符是否为字母,输出内容详见输出样例。

输入:

A

6

输出:

A is an alphabet.

6 is not an alphabet.

参考代码:

c 复制代码
#include <stdio.h>

int main()
{
    int ch = 0;
    while ((ch = getchar()) != EOF)
    {
        if (isalpha(ch))
        {
            printf("%c is an alphabet.\n", ch);
        }
        else
        {
            printf("%c is not an alphabet.\n", ch);
        }
        getchar();
    }
    return 0;
}
2.三角形判断

题目描述:

KK想知道已经给出的三条边a,b,c能否构成三角形,如果能构成三角形,判断三角形的类型(等边三角形、等腰三角形或普通三角形)。

输入描述:

题目有多组输入数据,每一行输入三个a,b,c(0<a,b,c<1000),作为三角形的三个边,用空格分隔。

输出描述:

针对每组输入数据,输出占一行,如果能构成三角形,等边三角形则输出"Equilateral triangle!",等腰三角形则输出"Isosceles triangle!",其余的三角形则输出"Ordinary triangle!",反之输出"Not a triangle!"。

输入:

2 3 2

3 3 3

输出:

Isosceles triangle!

Equilateral triangle!

参考代码:

c 复制代码
#include <stdio.h>

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    while (scanf("%d %d %d", &a, &b, &c) != EOF)
    {
        if (a + b > c || a + c > b || c + b > a)
        {
            if (a == b && b == c)
            {
                printf("Equilateral triangle!\n");
            }
            else if ((a==b && a!= c) || (a==c && a!=b) ||(b==c  &&  b!= a))
            {
                printf("Isosceles triangle!\n");
            }
            else
            {
                printf("Ordinary triangle!\n");
            }
        }
        else
        {
                printf("Not a triangle!\n");
        }
    }
    return 0;
}
3.衡量人体胖瘦程度

题目描述:

在计算BMI(BodyMassIndex ,身体质量指数)的案例基础上,判断人体胖瘦程度。BMI中国标准如下表所示。

BMI范围 分类
BMI<18.5 偏瘦(Underweight)
BMI>=18.5且BMI<=23.9 正常(Normal)
BMI>23.9且BMI<=27.9 过重(Overweight)
BMI>27.9 肥胖(Obese)

输入描述:

多组输入,每一行包括两个整数,用空格隔开,分别为体重(公斤)和身高(厘米)。

输出描述:

针对每行输入,输出为一行,人体胖瘦程度,即分类。

输入:

80 170

60 170

90 160

50 185

输出:

Overweight

Normal

Obese

Underweight

参考代码:

c 复制代码
#include <stdio.h>

int main()
{
    int h = 0;
    int w = 0;
    double bmi = 0.0;
    while (scanf("%d %d", &w, &h) != EOF)
    {
        bmi = w / ((h / 100.0) * (h / 100.0));
        if (bmi < 18.9)
            printf("Underweight\n");
        else if (bmi >= 18.5 && bmi <= 23.9)
            printf("Normal\n");
        else if (bmi > 23.9 && bmi <= 27.9)
            printf("Overweight\n");
        else
            printf("Obese\n");
    }
    return 0;
}
4.翻转金字塔图案

题目描述:

KK学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用"*"组成的翻转金字塔图案。

输入描述:

多组输入,一个整数(2~20),表示翻转金字塔边的长度,即"*"的数量,也表示输出行数。

输出描述:

针对每行输入,输出用""组成的金字塔,每个""后面有一个空格。

输入:

5

输出:

c 复制代码
* * * * * 
 * * * *
  * * * 
   * * 
    *

参考代码:

c 复制代码
#include <stdio.h>

int main()
{
    int n = 0;
    while (~scanf("%d", &n))
    {
        int i = 0;
        for (i = 0; i < n; i++)
        {
            int j = 0;
            for (j = 0; j < i; j++)
            {
                printf(" ");
            }

            for (j = 0; j < n-i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    return 0;
}
5.平均身高

题目描述:

从键盘输入5个人的身高(米),求他们的平均身高(米)。

输入描述:

一行,连续输入5个身高(范围0.00~2.00),用空格分隔。

输出描述:

一行,输出平均身高,保留两位小数。

输入:

1.68 1.75 1.82 1.60 1.92

输出:

1.75

参考代码:

c 复制代码
#include <stdio.h>

int main()
{
    float score = 0.0;
    float sum = 0.0;
    int i = 0;
    for (i = 0; i < 5; i++)
    {
        scanf("%f", &score);
        sum += score;
    }
    printf("%.2f\n", sum/5.0);
    return 0;
}
相关推荐
骁的小小站40 分钟前
Verilator 和 GTKwave联合仿真
开发语言·c++·经验分享·笔记·学习·fpga开发
心灵宝贝3 小时前
申威架构ky10安装php-7.2.10.rpm详细步骤(国产麒麟系统64位)
开发语言·php
lly2024063 小时前
PHP 字符串操作详解
开发语言
大数据张老师3 小时前
数据结构——邻接矩阵
数据结构·算法
低音钢琴4 小时前
【人工智能系列:机器学习学习和进阶01】机器学习初学者指南:理解核心算法与应用
人工智能·算法·机器学习
像是套了虚弱散4 小时前
DevEco Studio与Web联合开发:打造鸿蒙混合应用的全景指南
开发语言·前端·华为·harmonyos·鸿蒙
旭意4 小时前
C++蓝桥杯之结构体10.15
开发语言·c++
麦麦鸡腿堡6 小时前
Java的单例设计模式-饿汉式
java·开发语言·设计模式
简单点了6 小时前
go前后端项目的启动 、打包和部署
开发语言·后端·golang
傻童:CPU6 小时前
C语言需要掌握的基础知识点之前缀和
java·c语言·算法