分支-08. 高速公路超速处罚

来源:互联网 发布:淘宝改地址铜仁市 编辑:程序博客网 时间:2024/04/28 01:34

开始的程序为:

#include "stdio.h"int main(){    int v1,v2;    double Exceed;    scanf("%d %d",&v1,&v2);    Exceed = (v2 - v1) / v1;    if (Exceed < 0)        exit(1);    else if (Exceed >= 0 && Exceed < 0.1)        printf("OK\n");    else if (Exceed >= 0.1 && Exceed < 0.5)        printf("Exceed %d%%. Ticket 200\n",int (100 * Exceed));    else if (Exceed >= 0.5)        printf("Exceed %d%%. license Revoked\n",int (100 * Exceed));    return 0;}

其中Exceed的公式有问题,对照其他程序进行处理更改为

#include "stdio.h"int main(){    int v1,v2,Exceed;    scanf("%d %d",&v1,&v2);    Exceed = 1.0 * (v1 - v2) / v2 * 100 + 0.5;    if (Exceed < 0)        exit(1);    else if (Exceed < 10)        printf("OK\n");    else if (Exceed < 50)        printf("Exceed %d%%. Ticket 200\n",Exceed);    else        printf("Exceed %d%%. License Revoked\n",Exceed);    return 0;}
运行成功

关于0.5在整形计算中的使用要进行总结和记忆,四舍五入的计算表现形式;

另外关于if语句使用条件上可以简短说明的尽量省略;

细心 不出现License写成license的情况

0 0
原创粉丝点击