C语言练习题110例(四)

31.争夺前五名

题目描述:

期中考试开始了,大家都想取得好成绩,争夺前五名。从键盘输入n个学生成绩(不超过40个),输出每组排在前五高的成绩。

输入描述:

两行,第一行输入一个整数,表示n个学生(>=5),第二行输入n个学生成绩(整数表示,范围0~100),用空格分隔。

输出描述:

一行,输出成绩最高的前五个,用空格分隔。

输入:

6

99 45 78 67 72 88

输出:

99 88 78 72 67

参考代码:

//方法一
#include <stdio.h>

int main()
{
    int n = 0;
    int i = 0;
    int score[40] = { 0 };
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &score[i]);
    }

    for (i = 0; i < n; i++)
    {
        int j = 0;
        for (j = 0; j < n-i-1; j++)
        {
            if (score[j + 1] > score[j])
            {
                int tmp = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tmp;
            }
        }
    }

    for (i = 0; i < 5; i++)
    {
        printf("%d ", score[i]);
    }
    return 0;
}

//方法二
#include <stdio.h>
#include <stdlib.h>

int cmp(const void* e1, const void* e2)
{
    return *(int*)e2 - *(int*) e1;
}

int main()
{
    int n = 0;
    int i = 0;
    int score[40] = { 0 };
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &score[i]);
    }

    qsort(score, n, 4, cmp);

    for (i = 0; i < 5; i++)
    {
        printf("%d ", score[i]);
    }
    return 0;
}

32.竞选社长

题目描述:

假设你们社团要竞选社长,有两名候选人分别是A和B,社团每名同学必须并且只能投一票,最终得票多的人为社长.

输入描述:

一行,字符序列,包含A或B,输入以字符0结束。

输出描述:

一行,一个字符,A或B或E,输出A表示A得票数多,输出B表示B得票数多,输出E表示二人得票数相等。

输入:

ABBABBAAB0

输出:

B

参考代码:

#include <stdio.h>

int main()
{
    int flag = 0;
    int i = 0;
    char arr[100] = { 0 };
    gets(arr);
    while (arr[i] != '0')
    {
        if (arr[i] == 'A')
        {
            flag++;
        }
        if (arr[i] == 'B')
        {
            flag--;
        }
        i++;
    }

    if (flag > 0)
    {
        printf("A\n");
    }
    else if (flag < 0)
    {
        printf("B\n");
    }
    else
    {
        printf("E\n");
    }
    return 0;
}

33.你是天才吗?

题目描述:

据说智商140以上者称为天才,KK想知道他自己是不是天才,请帮他编程判断。输入一个整数表示一个人的智商,如果大于等于140,则表明他是一个天才,输出“Genius”。

输入描述:

多组输入,每行输入包括一个整数表示的智商。

输出描述:

针对每行输入,输出“Genius”。

输入:

160

输出:

Genius

参考代码:

#include <stdio.h>

int main()
{
    int n = 0;
    while (~scanf("%d", &n))
    {
        if (n >= 140)
        {
            printf("Genius\n");
        }
    }
    return 0;
}

34.完美成绩

题目描述:

KK想知道他的考试成绩是否完美,请帮他判断。从键盘输入一个整数表示的成绩,编程判断成绩是否在90~100之间,如果是则输出“Perfect”。

输入描述:

多组输入,每行输入包括一个整数表示的成绩(90~100)。

输出描述:

针对每行输入,输出“Perfect”。

输入:

98

输出:

Perfect

参考代码:

#include <stdio.h>

int main()
{
    int score = 0;
    while (~scanf("%d", &score))
    {
        if (score >= 90 && score <= 100)
        {
            printf("Perfect\n");
        }
    }
    return 0;
}

35.及格分数

题目描述:

KK想知道他的考试分数是否通过,请帮他判断。从键盘任意输入一个整数表示的分数,编程判断该分数是否在及格范围内,如果及格,即:分数大于等于60分,是输出“Pass”,否则,输出“Fail”。

输入描述:

多组输入,每行输入包括一个整数表示的分数(0~100)。

输出描述:

针对每行输入,输出”Pass" 或"Fail”。

示例1

输入:

94

输出:

Pass

示例2

输入:

44

输出:

Fail

参考代码:

#include <stdio.h>

int main()
{
    int score = 0;
    while (scanf("%d", &score) != EOF)
    {
        if (score >= 60)
        {
            printf("Pass\n");
        }
        else
        {
            printf("Fail\n");
        }
    }
    return 0;
}

36.判断整数的奇偶性

题目描述:

KiKi想知道一个整数的奇偶性,请帮他判断。从键盘任意输入一个整数(范围-231~231-1),编程判断它的奇偶性。

输入描述:

多组输入,每行输入包括一个整数。

输出描述:

针对每行输入,输出该数是奇数(Odd)还是偶数(Even)。

输入:

4

7

输出:

Even

Odd

参考代码:

#include <stdio.h>

int main()
{
    int n = 0;
    while (~scanf("%d", &n))
    {
        if (n % 2 == 1)
        {
            printf("Odd\n");
        }
        else
        {
            printf("Even\n");
        }
    }
    return 0;
}

37.最高分数

题目描述:

KK参加了语文、数学、外语的考试,请帮他判断三科中的最高分。从键盘任意输入三个整数表示的分数,编程判断其中的最高分。

输入描述:

多组输入,每行输入包括三个整数表示的分数(0~100),用空格分隔。

输出描述:

针对每行输入,输出为一行,即三个分数中的最高分。

输入:

94 98 99

100 88 60

输出:

99

100

参考代码:

#include <stdio.h>

int main()
{
    int s1 = 0;
    int s2 = 0;
    int s3 = 0;
    int max = 0;
    while (~scanf("%d %d %d", &s1, &s2, &s3))
    {
        max = s1 > s2 ? s1 : s2;
        max = max > s3 ? max : s3;
        printf("%d\n", max);
    }
}

38.判断元音还是辅音

题目描述:

KK开始学习英文字母,BoBo老师告诉他,有五个字母A(a), E(e), I(i), O(o),U(u)称为元音,其他所有字母称为辅音,请帮他编写程序判断输入的字母是元音(Vowel)还是辅音(Consonant)。

输入描述:

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

输出描述:

针对每组输入,输出为一行,如果输入字母是元音(包括大小写),输出“Vowel”,如果输入字母是非元音,输出“Consonant"。

输入:

A

b

输出:

Vowel

Consonant

参考代码:

#include <stdio.h>

int main()
{
    char arr[] = "AaEeIiOoUu";
    char ch = 0;
    int i = 0;
    while (scanf(" %c", &ch) != EOF)
    {
        for (i = 0; i < 10; i++)
        {
            if (ch == arr[i])
            {
                printf("Vowel\n");
                break;
            }
        }
        if (i == 10)
        {
            printf("Consonant\n");
        }
    }
    return 0;
}

39.判断是不是字母

题目描述:

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

输入描述:

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

输出描述:

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

输入:

A

6

输出:

A is an alphabet.

6 is not an alphabet.

参考代码:

#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;
}

40.字母的大小写转换

题目描述:

KK想完成字母大小写转换,有一个字符,判断它是否为大写字母,如果是,将它转换成小写字母;反之则转换为大写字母。

输入描述:

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

输出描述:

针对每组输入,输出单独占一行,输出字母的对应形式。

输入:

a

A

Z

输出:

A

a

z

参考代码:

#include <stdio.h>

int main()
{
    char ch = 0;
    while(scanf(" %c", &ch) != EOF)
    {
        if (islower(ch))
        {
            printf("%c\n", toupper(ch));
        }
        else
        {
            printf("%c\n", tolower(ch));
        }
    }
    return 0;
}

C语言练习题110例【41-50】

  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值