题解 | 学生基本信息输入输出

学生基本信息输入输出

https://www.nowcoder.com/practice/58b6a69b4bf943b49d2cd3c15770b9fd

第一种方法使用round()函数

round()数学四舍五入函数,专门把小数变成最近的整数

规则:

  • 小数部分 ≥ 0.5进 1
  • 小数部分 < 0.5舍去
#include <stdio.h>
#include <math.h>

int main() {
    long long i;
    double a,b,c;
    scanf("%lld;%lf,%lf,%lf",&i, &a, &b,&c);
    a = round(a * 100) / 100;
    b = round(b * 100) / 100;
    c = round(c * 100) / 100;
    printf("The each subject score of No. %lld is %.2f, %.2f, %.2f.", i, a, b,c);
    
    return 0;
}

第二种不用引入库函数的方法:

#include <stdio.h>

int main() {
    long long i;
    double a, b, c;
    scanf("%lld;%lf,%lf,%lf", &i, &a, &b, &c);

    // 不用round(),手动实现四舍五入到两位小数
    a = (int)(a * 100 + 0.5) / 100.0;
    b = (int)(b * 100 + 0.5) / 100.0;
    c = (int)(c * 100 + 0.5) / 100.0;

    printf("The each subject score of No. %lld is %.2f, %.2f, %.2f.", i, a, b, c);
    return 0;
}

全部评论

相关推荐

在下uptown:哈哈哈哈,大家仿佛形成了AI项目+商城的统一套路[笑cry不过该说不说整体还可以
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务