杭電acm 1302 蝸牛爬井

#include<stdio.h>  
#include<string.h>    
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<iostream>
using namespace std;
static int i=1;
int cmd(double h,double u,double d,double f)
{
    double t=h;
    if(u<h)
    {
        h=h-u+d;
        u=(1-f/100)*u;
        i++;
        if(h==t)
        {
            cout<<"failure on day "<<i-1<<endl;
        }
        return cmd(h,u,d,f);
    }
    else
    {
        cout<<"success on day "<<i<<endl;
    }
}
int main()
{
    double h,u,d,f;
    while(cin>>h&&h!=0)
    {
        cin>>u>>d>>f;
        cmd(h,u,d,f);       
    }
    return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章