MOOC 苏小红C语言 第四周编程题

点我查看MOOC苏小红C语言程序设计精髓所有编程题

目录

1.检测用户错误输入(4分)

2.闰年判断(6分)

3.程序改错v1.0(7分)

4.字符类型判断(4分)

5.快递费用计算(7分)

6.数位拆分v2.0(4分)

7.出租车计价(4分)

8.数据区间判断(6分)

9.计算一元二次方程的根v2.0(4分)


1.检测用户错误输入(4分)

题目内容:

根据scanf()的返回值判断scanf()是否成功读入了指定的数据项数,使程序在用户输入123a时,能输出如下运行结果:

123a↙

 

Input error!

 

输入格式: "%d %d"

输出格式:

如果成功读入指定的数据项数,输出格式为:"a = %d, b = %d\n" (注意:等号的两边各有一个空格)

输入非法数据,输出格式为:"Input error!"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int m, n;
    // scanf()函数的返回值是正确读入数据的个数
    int k = scanf("%d %d", &m, &n);
    if (k == 2) {
        printf("a = %d, b = %d\n", m, n);
    } else{
        printf("Input error!");
    }
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过2ms256kb 2
用例2通过2ms256kb 2

提交答案

本次得分/总分:4.00/4.00分


2.闰年判断(6分)

题目内容:

从键盘任意输入一个公元年份(大于等于1),判断它是否是闰年。若是闰年输出“Yes”,否则输出“No”。要求对输入数据进行合法性判断。

 

已知符合下列条件之一者是闰年:

(1)能被4整除,但不能被100整除;

(2)能被400整除。

 

运行结果示例1:

2015↙

No

 

运行结果示例2:

2016↙

Yes

 

运行结果示例3:

-123↙

Input error!

 

运行结果示例4:

a↙

Input error!

 

输入格式: "%d"

输出格式:

是闰年,输出:"Yes\n"

不是闰年,输出:"No\n"

输入数据不合法,输出:"Input error!\n"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int year;
    int k = scanf("%d", &year);
    // 读入的年只能是正整数, %d可确保读入的是整数,但无法保证是正整数
    // 所以需要判断
    if (k != 1 || year < 0) {
        printf("Input error!\n");
    } else if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过1ms256kb 1
用例2通过1ms256kb 1
用例3通过2ms256kb 1
用例4通过1ms256kb 1
用例5通过1ms256kb 1
用例6通过2ms256kb 1

提交答案

本次得分/总分:6.00/6.00分


3.程序改错v1.0(7分)

题目内容:

下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,否则将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。

#include<stdio.h>
int main()
{
    int score;
    char grade;
    printf("Please input  score:");
    scanf("%d", &score);
    if (score < 0 || score > 100)
        printf("Input error!\n");
    else if (score >= 90)
        grade = 'A’;
    else if (score >= 80)
        grade = 'B';
    else if (score >= 70)
        grade = 'C';
    else if (score >= 60)
        grade = 'D';
    else
        grade = 'E';
    printf("grade:%c\n", grade);
    return 0;
}

程序运行结果示例1:

Please input score:

-1↙

Input error!

 

程序运行结果示例2:

Please input score:

95↙

grade: A

 

程序运行结果示例3:

Please input score:

82↙

grade: B

 

程序运行结果示例4:

Please input score:

72↙

grade: C

 

程序运行结果示例5:

Please input score:

66↙

grade: D

 

程序运行结果示例6:

Please input score:

32↙

grade: E

 

程序运行结果示例7:

Please input score:

127↙

Input error!

 

输入提示信息:"Please input score:\n"

输入格式: "%d"

输出格式:

用户输入存在错误时,输出提示信息:"Input error!\n"

输出格式:"grade: %c\n" (注意:%c前面有一个空格)

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include<stdio.h>

int main() {
    int score;
    char grade;
    printf("Please input score:\n");
    int k = scanf("%d", &score);
    if (score < 0 || score > 100 || k != 1) {
        printf("Input error!\n");
        // 输入有错直接终止程序, 否则会输出第23行
        // 又或者设置一个标记变量,根据标记变量判断第23是否执行
        return 0;
    } else if (score >= 90) {
        grade = 'A';
    } else if (score >= 80) {
        grade = 'B';
    } else if (score >= 70) {
        grade = 'C';
    } else if (score >= 60) {
        grade = 'D';
    } else {
        grade = 'E';
    }
    printf("grade: %c\n", grade);
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过1ms256kb 1
用例2通过1ms128kb 1
用例3通过2ms256kb 1
用例4通过1ms256kb 1
用例5通过1ms256kb 1
用例6通过1ms256kb 1
用例7通过2ms256kb 1

提交答案

本次得分/总分:7.00/7.00分


4.字符类型判断(4分)

题目内容:

从键盘键入任意一个字符,判断该字符是英文字母(不区分大、小写)、数字字符还是其它字符。

若键入字母,则屏幕显示 It is an English character.;若键入数字则屏幕显示It is a digit character. ;若输入其它字符,则屏幕显示:It is other character. 

程序的运行示例1:

Input simple:

b

It is an English character.

 

程序的运行示例2:

Input simple:

6

It is a digit character.

 

程序的运行示例3:

Input simple:

*

It is other character.

 

 

程序的运行示例4:

Input simple:

A↙

It is an English character.

 

输入信息提示:"Input simple:\n"

输入格式:  "%c"

输出格式:

英文字符的输出格式:"It is an English character.\n"

数字的输出格式:"It is a digit character.\n"

其它字符的输出格式:"It is other character.\n"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include<stdio.h>

int main() {
    char ch;
    printf("Input simple:\n");
    scanf("%c", &ch);
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        printf("It is an English character.\n");
    } else if (ch >= '0' && ch <= '9') {
        printf("It is a digit character.\n");
    } else {
        printf("It is other character.\n");
    }
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过1ms256kb 1
用例2通过2ms256kb 1
用例3通过1ms256kb 1
用例4通过1ms256kb 1

提交答案

本次得分/总分:4.00/4.00分


5.快递费用计算(7分)

题目内容:

上海市的某快递公司根据投送目的地距离公司的远近,将全国划分成5个区域:

 

0区

1区

2区

3区

4区

同城

临近两省

1500公里(含)以内

1500——2500公里

2500公里以上

上海

江苏,浙江

北京,天津,河北,辽宁,河南,安微,陕西,湖北,江西,湖南,福建,广东,山西。

吉林,辽宁,甘肃,四川,重庆,青海,广西,云南,海南,内蒙古,黑龙江,贵州。

新疆,西藏。

 

 

快递费按邮件重量计算,由起重费用、续重费用两部分构成:

(1) 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;

(2) 同城起重资费10元,续重3元/公斤;

(3) 寄往1区(江浙两省)的邮件,起重资费10元,续重4元;

(4) 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:2区的续重5元/公斤,3区的续重6.5元/公斤,4区的续重10元/公斤。

 

编写程序,从键盘输入邮件的目的区域编码和重量,计算并输出运费,计算结果保留2位小数。程序中所有浮点数的数据类型均为float。

提示:续重部分不足一公斤,按1公斤计算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算续重,不足1公斤按1公斤计算,1.3公斤折合续重为2公斤。如果重量应大于0、区域编号不能超出0-4的范围。

 

程序运行结果示例1:

4,4.5↙

Price: 55.00

 

程序运行结果示例2:

5,3.2↙

Error in Area

Price:  0.00

 

输入格式:

用逗号分隔的两个数字,第一个表示区域、第二个是重量:"%d,%f"

 

输出格式:

价格的输出格式:"Price: %5.2f\n"

区域错误的提示信息:"Error in Area\n"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include<stdio.h>

/**
 *
 * @param weight 物品总重量
 * @param baseWeight 首重
 * @param addionalWeight 续重
 */
void getBaseWeightAndAddionalWeight(const int weight, float *baseWeight,float *addionalWeight){
    if (weight >= 1) {
        *baseWeight = 1;
        *addionalWeight = (int)(weight - *baseWeight + 1) * 1.0;
    } else {
        *baseWeight = 1;
        *addionalWeight = 0;
    }
}
int main() {
    float baseWeight;
    float addionalWeight;
    float weight;
    int area;
    scanf("%d,%f", &area, &weight);
    if (area < 0 || area > 5) {
        printf("Error in Area\n");
    }
    getBaseWeightAndAddionalWeight(weight, &baseWeight, &addionalWeight);
    float price = 0;
    switch (area) {
        case 0:
            price = 10 * baseWeight + 3 * addionalWeight;
            break;
        case 1:
            price = 10 * baseWeight + 4 * addionalWeight;
            break;
        case 2:
            price = 15 * baseWeight + 5 * addionalWeight;
            break;
        case 3:
            price = 15 * baseWeight + 6.5 * addionalWeight;
            break;
        case 4:
            price = 15 * baseWeight + 10 * addionalWeight;
            break;
    }
    printf("Price: %5.2f\n", price);
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过1ms256kb 1
用例2通过1ms136kb 1
用例3通过1ms256kb 1
用例4通过1ms256kb 1
用例5通过1ms256kb 1
用例6通过1ms256kb 1
用例7通过2ms256kb 1

提交答案

本次得分/总分:7.00/7.00分


6.数位拆分v2.0(4分)

题目内容:

从键盘上输入一个4位数的整数n,编写程序将其拆分为两个2位数的整数a和b,计算并输出拆分后的两个数的加、减、乘、除和求余运算的结果。例如n=-4321,设拆分后的两个整数为a,b,则a=-43,b=-21。除法运算结果要求精确到小数点后2位,数据类型为float。求余和除法运算需要考虑除数为0的情况,即如果拆分后b=0,则输出提示信息"The second operater is zero!"

 

程序的运行结果示例1:

Please input n:

1200↙

12,0

sum=12,sub=12,multi=0

The second operator is zero!

 

程序的运行结果示例2:

Please input n:

-2304↙

-23,-4

sum=-27,sub=-19,multi=92

dev=5.75,mod=-3

 

输入提示信息:"Please input n:\n"

输入格式: "%d"

输出格式:

拆分后的两个整数的输出格式:"%d,%d\n"

加法、减法、乘法的输出格式:"sum=%d,sub=%d,multi=%d\n"

除法和求余的输出格式:"dev=%.2f,mod=%d\n"

除数为0的提示信息:"The second operator is zero!\n"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

 

时间限制:500ms内存限制:32000kb

C

#include "stdio.h"
#include "math.h"
int main() {
    int n;
    printf("Please input n:\n");
    scanf("%d", &n);
    int a = n / 100, b = n % 100;
    printf("%d,%d\n", a, b);
    printf("sum=%d,sub=%d,multi=%d\n", a + b, a - b, a * b);
    if (b == 0) {
        printf("The second operator is zero!\n");
    } else {
        printf("dev=%.2f,mod=%d\n", a * 1.0 / b, a % b);
    }
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过2ms256kb 2
用例2通过1ms128kb 1
用例3通过1ms256kb 1

提交答案

本次得分/总分:4.00/4.00分


7.出租车计价(4分)

题目内容:

已知某城市普通出租车收费标准为:起步里程为3公里,起步费为8元,10公里以内超过起步里程的部分,每公里加收2元,超过10公里以上的部分加收50%的回空补贴费,即每公里3元。出租车营运过程中,因堵车和乘客要求临时停车等客的,按每5分钟加收2元

计算,不足5分钟的不计费。从键盘任意输入行驶里程(精确到0.1公里)和等待时间(精确到分钟),请编程计算并输出乘客应支付的车费,对结果进行四舍五入,精确到元。

 

程序运行结果示例1:

Input distance and time:2,2↙

fee = 8

 

程序运行结果示例2:

Input distance and time:5,5↙

fee = 14

 

程序运行结果示例3:

Input distance and time:12,15↙

fee = 34

 

程序运行结果示例4:

Input distance and time:20,0↙

fee = 52

 

输入提示信息:"Input distance and time:"

输入格式:

用逗号分隔的两个数字,第一个表示距离、第二个表示时间:"%f,%d"

输出格式:"fee = %.0f\n"   (注意:等号的两边各有一个空格)

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

 

时间限制:500ms内存限制:32000kb

C

#include "stdio.h"
#include "math.h"
int main() {
    float dis;
    int minute;
    float price = 0;
    printf("Input distance and time:");
    scanf("%f,%d", &dis, &minute);

    // 按照规则计算里程费用
    if (dis <= 3) {
        price = 8;
    } else if (dis <= 10) {
        price = 8 + (dis - 3) * 2;
    } else {
        price = 8 + 7 * 2 + (dis - 10) * 3;
    }

    // 不足五分钟不计分,即只计算五的倍数
    int time = minute / 5;
    price = price + 2*time;

    // 计算四舍五入
    float t = price - (int)price;
    if (t >= 0.5) {
        price = (int) price + 1;
    } else {
        price = (int) price;
    }

    // 输出价格
    printf("fee = %.0f\n", price);
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过1ms128kb 1
用例2通过1ms128kb 1
用例3通过1ms128kb 1
用例4通过1ms128kb 1

提交答案

本次得分/总分:4.00/4.00分


8.数据区间判断(6分)

题目内容:

从键盘输入一个int型的正整数n(已知:0<n<10000),编写程序判断n落在哪个区间。如果用户输入的数据不在指定的范围里,程序输出 "error!"。例如,输入265,则该数属于区间 100-999。

 

程序运行结果示例1:

Please enter the number:

2563↙

2563: 1000-9999

 

程序运行结果示例2:

Please enter the number:

156↙

156: 100-999

 

程序运行结果示例3:

Please enter the number:

36↙

36: 10-99

 

程序运行结果示例4:

Please enter the number:

3↙

3: 0-9

 

程序运行结果示例5:

Please enter the number:

10923↙

error!

 

输入提示信息:"Please enter the number:\n"

输入错误提示信息:"error!\n"

输入格式: "%d"

输出格式:

输出的区间判断:

"%d: 1000-9999\n"

"%d: 100-999\n"

"%d: 10-99\n"

"%d: 0-9\n"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include "stdio.h"
#include "math.h"
int main() {
    int n;
    printf("Please enter the number:\n");
    scanf("%d", &n);
    if(n < 1) { // 输入的n小于等于0时,不合法
        printf("error!\n");
    }else if (n < 10) {
        printf("%d: 0-9\n", n);
    } else if (n < 100) {
        printf("%d: 10-99\n", n);
    } else if (n < 1000) {
        printf("%d: 100-999\n", n);
    } else if (n < 10000) {
        printf("%d: 1000-9999\n", n);
    } else { // 其余的,不合法
        printf("error!\n");
    }
    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过2ms256kb 1
用例2通过1ms256kb 1
用例3通过2ms256kb 1
用例4通过1ms256kb 1
用例5通过2ms256kb 1
用例6通过2ms256kb 1

提交答案

本次得分/总分:6.00/6.00分

9.计算一元二次方程的根v2.0(4分)

题目内容:

根据下面给出的求根公式,计算并输出一元二次方程的两个实根,要求精确到小数点后4位。其中a,b,c的值由用户从键盘输入。如果用户输入的系数不满足求实根的要求,输出错误提示 "error!"。程序中所有的数据类型均为float。

 

 

程序运行结果示例1:

Please enter the coefficients a,b,c:

1,2,1↙

x1=-1.0000, x2=-1.0000

 

程序运行结果示例2:

Please enter the coefficients a,b,c:

2,6,1↙

x1=-0.1771, x2=-2.8229

 

程序运行结果示例3:

Please enter the coefficients a,b,c:

2,1,6↙

error!

 

输入提示信息:"Please enter the coefficients a,b,c:\n"

输入格式: "%f,%f,%f"

输出格式: "x1=%7.4f, x2=%7.4f\n"

如果输入的系数不满足求实根的要求,输出错误提示信息:"error!\n"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

C

#include "stdio.h"
#include "math.h"
int main() {
    printf("Please enter the coefficients a,b,c:\n");
    float a = 2, b = 3, c = 1;
    scanf("%f,%f,%f", &a, &b, &c);
    if ((b * b - 4 * a * c) < 0) {
        printf("error!\n");
        return 0;
    }
    float x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
    float x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);

    printf("x1=%7.4f, x2=%7.4f\n", x1, x2);

    return 0;
}
用例测试结果运行时间占用内存提示得分
用例1通过3ms256kb 1
用例2通过1ms128kb 1
用例3通过1ms128kb 1
用例4通过1ms256kb 1

提交答案

本次得分/总分:4.00/4.00分

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eva_5433

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值