【C语言每日一题】编程题入门篇-002

一、分段函数求值

题目描述

有一个函数
y={  x      x<1
    |  2x-1   1<=x<10
    { 3x-11  x>=10

写一段程序,输入x,输出y

输入格式

一个数x

输出格式

一个数y

样例输入

14

样例输出

31
#include"stdio.h"
int main()
{
    int x, y;
    scanf("%d",&x);
    if(x < 1)
    {
        y = x;
    } else if(x >= 10)
    {
        y = 3*x-11;

    } else
    {
        y = 2*x-1;
    }
    printf("%d\n", y);
    return 0;
}

二、成绩评定

题目描述

给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。 90分以及90分以上为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。

输入格式

一个整数0-100以内

输出格式

一个字符,表示成绩等级

样例输入

90

样例输出

A
#include <stdio.h>

int main() {
    int grade;
    char level;

    scanf("%d", &grade);

    if (grade >= 90) {
        level = 'A';
    } else if (grade >= 80 && grade <= 89) {
        level = 'B';
    } else if (grade >= 70 && grade <= 79) {
        level = 'C';
    } else if (grade >= 60 && grade <= 69) {
        level = 'D';
    } else {
        level = 'E';
    }

    printf("%c\n", level); 

    return 0;
}

三、利润计算

题目描述

企业发放的奖金根据利润I提成。

利润I低于或等于100000元的,奖金可提10%;
利润高于100000元,低于或等于200000元(100000<I≤200000)时,低于等于100000元部分按10%提成,高于100000元的部分,可提成 7.5%;
200000<I≤400000时,低于200000元部分仍按上述办法提成(下同),高于200000元的部分按5%提成;
400000<I≤600000元时,高于400000元的部分按3%提成;

600000<I≤1000000时,高于600000元的部分按1.5%提成;
I>1000000时,超过1000000元的部分按1%提成。

从键盘输入当月利润I,求应发奖金总数。

输入格式

一个整数,当月利润。

输出格式

一个整数,奖金。

样例输入

900

样例输出

90
#include "stdio.h"
int main()
{
    int i,r;
    const int B = 100000;
    scanf("%d",&i);
    if(i<=B)//利润低于或等于100000元时
    {
        r = i*0.1;
    }
    else if(i>B && i<=(2*B))     //利润高于100000元,低于或等于200000元时
    {
        r = 0.1*B+(i-B)*0.075;
    }
    else if(i>(2*B) && i<=(4*B))     //利润高于200000元,低于或等于400000元时
    {
        r = 0.1*B+0.075*B+(i-2*B)*0.05;
    }
    else if(i>(4*B) && i<=(6*B))     //利润高于400000元,低于或等于600000元时
    {
        r = 0.1*B+0.075*B+0.05*2*B+(i-4*B)*0.03;
    }
    else if(i>(6*B) && i<=(10*B))     //利润高于600000元,低于或等于1000000元时
    {
        r = 0.1*B+0.075*B+0.05*2*B+0.03*2*B+(i-6*B)*0.015;
    }
    else if(i>(10*B))     //利润高于1000000元时
    {
        r = 0.1*B+0.075*B+0.05*2*B+0.03*2*B+4*B*0.015+((i-10*B)*0.01);
    }
    printf("%d", r);
    return 0;
}

四、字符串分类统计

题目描述

输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。

输入格式

一行字符,长度不超过200

输出格式

统计值

样例输入

aklsjflj123 sadf918u324 asdf91u32oasdf/.';123

样例输出

23 16 2 4
#include <stdio.h>

int main() {
    int letter = 0, number = 0, blank = 0, others = 0;
    int c;

    while ((c = getchar()) != '\n' && c != EOF) { // 添加对EOF的处理,以防止文件结束时的问题
        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { // 使用括号使逻辑更清晰
            letter++;
        } else if (c >= '0' && c <= '9') {
            number++;
        } else if (c == ' ') {
            blank++;
        } else {
            others++;
        }
    }

    printf("%d %d %d %d\n", letter, number, blank, others);

    return 0;
}

五、Sn的公式求和

题目描述

求Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字,为2。 例如,n=5时=2+22+222+2222+22222,n由键盘输入。

输入格式

n

输出格式

Sn的值

样例输入

5

样例输出

24690
#include <stdio.h>
int main() {
    int n, a = 2, sum = 0, currentTerm = 0;

    // 读取 n 的值
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        // 如果是第一项,则直接设置为 a
        if (i == 1) {
            currentTerm = a;
        } else {
            // 对于后续项,乘以 10 并加上 a
            currentTerm = currentTerm * 10 + a;
        }
        // 将当前项加入总和
        sum += currentTerm;
    }

    // 输出 Sn 的值
    printf("%d\n", sum);

    return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值