问答详情
源自:5-14 综合练习

/...算出来68问题在哪里

#include <stdio.h>
int cost(double distance,int time)
{
    double sum;
    if(distance<=3)
    {
        sum=13;
    }
    else if(distance>3)
    {
        if(time<=5||time>23)
        {
            sum=13+(distance-3)*2.3*1.2;
        }
        else if(time>5&&time<=23)
        {
            sum=13+(distance-3)*2.3;
        }
    }
    return sum+1;
}
int main()
{
    printf("小明路费为%d\n",cost(12,18)+cost(12,9));
    return 0;
}

提问者:qq_tor_1 2018-09-03 12:39

个回答

  • 清顔
    2018-09-04 09:14:50
    已采纳

    你自定义函数的时候就写的是整型,把int cost(double distance,int time)改成double cost(double distance,double time),然后再把printf中的%d改成%f就不会报错

  • 灵魂躯壳
    2018-09-06 19:41:00

    #include <stdio.h>

    float totalprice(int time,int distance)

    {

        float perprice=2.3,totalprice;

        if(time>=5&&time<23);

        else

            perprice+=perprice*0.2;

        if(distance<=3)

            return 14;

        else

        {

            totalprice=13+(distance-3)*perprice+1;

            return totalprice;

        }

    }

    int main()

    {

        int time1=9,time2=18,distance=12;

        float price1=totalprice(time1,distance);

        float price2=totalprice(time2,distance);

        printf("%.2f",price1+price2);

        return 0;

        

    }


  • qq_tor_1
    2018-09-03 12:42:09

    并且将printf中%d改为%f会报错