算法刷题-杭电2002

http://acm.hdu.edu.cn/showproblem.php?pid=2002

Problem Description
根据输入的半径值,计算球的体积。
 
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
 
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
 
Sample Input
1 1.5
 
Sample Output
4.189 14.137
Hint
#define PI 3.1415927
 
先这么写
#include <stdio.h>
#include "math.h"

#define PI 3.1415927

int main()
{
    double a;
    while (EOF != scanf("%f", &a))
    {
        double v = 4/3*PI*pow(a,3);
        printf("%.3f\n",v);
    }
    return 0;
}

代码运行结果不对,发现4/3出问题了。

改后

#include <stdio.h>
#include "math.h"

#define PI 3.1415927

int main()
{
    double a;
    while (EOF != scanf("%f", &a))
    {
        double v = 4.0/3*PI*pow(a,3);
        printf("%.3f\n",v);
    }
    return 0;
}

提交代码一直是wrong answer,查了下才知道,输入精度丢失了

#include <stdio.h>
#include "math.h"

#define PI 3.1415927

int main()
{
    double a;
    while (EOF != scanf("%lf", &a))
    {
        double v = 4.0/3*PI*pow(a,3);
        printf("%.3f\n",v);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Bejadin/p/8266658.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值