“21天好习惯”第一期-16

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

题目内容:

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

123a↙

Input error!

输入格式: "%d %d"

输出格式:

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

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

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

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

C

#include<stdio.h>
int main(){
    int a,b,x;
    x=scanf("%d %d",&a,&b);
    if(x==2)
         printf("a = %d, b = %d\n",a,b);
    else 
         printf("Input error!");
    return 0;
}

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>
int main()
{
    int year,x;
    x=scanf("%d",&year);
    if(x==1&&year>0)
    {
        if((year%4==0&&year%100!=0)||year%400==0)
        printf("Yes\n");
        else
        printf("No\n");
    }
     else
        printf("Input error!\n");
    return 0;
}

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,x;
           char grade;
           printf("Please input score:\n");
           x=scanf("%d", &score);
           if (score < 0 || score > 100 || x!=1){
                 printf("Input error!\n");
                 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;
    }

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 a;
    printf("Input simple:\n");
    scanf("%c",&a);
    if(a<='9'&&a>='0')
        printf("It is a digit character.\n");
    else if((a<='Z'&&a>='A')||(a<='z'&&a>='a'))
        printf("It is an English character.\n");
    else
        printf("It is other character.\n");
    return 0;
}

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>
int main()
{
    int a;
    float w,p;
    scanf("%d,%f",&a,&w);
    if(a<0||a>4||w<=0)
        printf("Error in Area\n");
    else
        {
            w=(int)w==w?w:(int)(w+1);
            switch(a)
            {
            case 0:p=10+(int)(w-1)*3;break;
            case 1:p=10+(int)(w-1)*4;break;
            case 2:p=15+(int)(w-1)*5;break;
            case 3:p=15+(int)(w-1)*6.5;break;
            default:p=15+(int)(w-1)*10;break;
            }
        }
    printf("Price: %5.2f\n",p);
    return 0;
}

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>
int main(){
    int n,a,b;
    printf("Please input n:\n");
    scanf("%d",&n);
    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",(float) a / b,a % b);
    }
    return 0;
}

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"   (注意:等号的两边各有一个空格)

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

#include <stdio.h>
int main()
{
    float x,f=0;
    int t;
    printf("Input distance and time:");
    scanf("%f,%d",&x,&t);
    t=t/5;
    f=f+2*t;
    if(x>10)
    {
        f=f+3*(x-10);
    }
    if(x>3)
    {
        x=x>=10?10:x;
        f=f+2*(x-3);
    }
    if(x>0)
    {
        f=f+8;
    }
    printf("fee = %.0f\n",f);
    return 0;
}

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>
int main()
{
    int n;
    printf("Please enter the number:\n");
    scanf("%d",&n);
    if(n > 0 && n < 10000)
    {
        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
        {
            printf("%d: 1000-9999\n",n);
        }
    } 
    else
    {
        printf("error!\n");
    }
    return 0;
}

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()
{
    float a,b,c,x1,x2;
    printf("Please enter the coefficients a,b,c:\n");
    scanf("%f,%f,%f",&a,&b,&c);
    if(sqrt(b*b-4*a*c)>=0)
    {
        x1=(-b+sqrt(b*b-4*a*c))/(2*a);
        x2=(-b-sqrt(b*b-4*a*c))/(2*a);
        printf("x1=%7.4f, x2=%7.4f\n",x1,x2);
    }
    else
    {
        printf("error!\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值