杭电acm 1302 蜗牛爬井

#include  
#include    
int main()  
{  
    double H,U,D,F;  
    while(~scanf("%lf%lf%lf%lf",&H,&U,&D,&F) && (H||U||D||F))  
    {  
        int day = 0;  
        double distance = 0,S = U*F/100;  
        bool flag = true;  
        while(1)  
        {  
            day++;  
            distance += U;  
            if(distance > H)  
                break;  
            distance -= D;  
            if(distance < 0)  
            {  
                flag = false;  
                break;  
            }  
            U -= S;  
            if(U < 0)  
                U = 0;  
        }  
        if(flag)  
            printf("success on day %d\n",day);  
        else  
            printf("failure on day %d\n",day);  
    }  

    return 0;  
}  

我先写的递归,时间超限

#include
using namespace std;
static int i=1;
int cmd(double h,double u,double d,double f)
{
    double t=h;
    if(u1-f/100)*u;
        i++;
        if(h==t)
        {
            cout<<"failure on day "<1<return cmd(h,u,d,f);
    }
    else
    {
        cout<<"success on day "<int main()
{
    double h,u,d,f;
    while(cin>>h&&h!=0)
    {
        cin>>u>>d>>f;
        cmd(h,u,d,f);       
    }
    return 0;
} 

你可能感兴趣的:(杭电acm 1302 蜗牛爬井)