Page 1 of 4

573 - The Snail

Posted: Thu Jun 27, 2002 11:39 pm
by zakaria
#include<iostream.h>

int main()
{
double h,u,d,f;
while(cin>>h>>u>>d>>f&&h>0)
{
double ih=0;
double hc=ih+u;
double hs=hc-d;
double fa=(u*f)/100.00;
long day=1;
int a=0,b=0;
do
{
ih=hs;
u-=fa;
if(u>=0)
{
hc=ih+u;
}
else hc=ih;
hs=hc-d;
day++;

if(hs<0||hc>=h)b=1;
if(hs<0)a=1;

}while(b!=1);
if(a==0)
cout<<"success on day "<<day<<"\n";
else
cout<<"failure on day "<<day<<"\n";
}
return 0;
}

Posted: Fri Jun 28, 2002 12:37 am
by Kamp
Pls specify the number of this problem :)

Good Luck :D

Posted: Fri Jun 28, 2002 10:19 am
by zakaria
Thanks for reply.
The number of this problem is 573.

Posted: Fri Jun 28, 2002 11:06 am
by Picard
try these inputs:
10 3 4 14
1 9 4 46

first will fail on the first day (not second), second will fail too, because it only reaches the top, but doesn't "exceed the height of the well".

Posted: Fri Jun 28, 2002 11:12 am
by Picard
sorry :roll: the previous second sample was a mistake. it's successful on the first day.
here is the input for just missing to exceed the height of the well:
9 8 1 75

Posted: Sat Jun 29, 2002 5:33 am
by zakaria
Yes it got accepted.
Thanks for your reply.

573

Posted: Wed Jul 10, 2002 3:40 pm
by yahoo
I can't why i have done the wrong. I think my algorithm is ok. Can anybody help me with any typical data.Here is my code:
#include <stdio.h>
#include <stdlib.h>

getData(char *str);
void calculate(int height, int climb, int slide, int fatigue);
int pos;

main(){
char buff[101];
int height, climb, slide, fatigue;

while(1){
pos = 0;
gets(buff);
height = getData(buff);
if (!height)
break;
climb = getData(buff);
slide = getData(buff);
fatigue = getData(buff);
calculate(height,climb,slide,fatigue);

}
return 0;
}

getData(char *str){

int i,j;
char valstr[4];
int value;

for (j = 0,i = pos;((str[i] != ' ') && (str[i] != '\0')) ;i++,j++){
valstr[j] = str[i];
}
valstr[j] = 0;
pos = i + 1;
value = atoi(valstr);
return value;

}
void calculate(int height, int climb, int slide, int fatigue){
int day = 1;
float total = 0.0;
float dayclimb, decline;

decline = (float)(climb)*fatigue/(float)100;
dayclimb = (float)climb;
while(1){
dayclimb = (float)(climb) - (day - 1)*decline;
total += dayclimb;
if (total > (float)(height)){
printf("success on day %d\n",day);
break;
}
else
total -= (float)(slide);
if (total < 0.0){
printf("failure on day %d\n",day);
break;
}
day++;
}
}
/* @END_OF_SOURCE_CODE */

Posted: Wed Jul 10, 2002 3:51 pm
by 10153EN
Have you searched the Volume V for a thread about this problem? There should be one about it and the content can answer your question.

Please searched the forum before posting question.

Posted: Sat Jul 13, 2002 8:06 am
by yahoo
Woubld you please explain what do you mean by searching thread about this problem? As a new programmer it is unknown to me.

573 The Snail Why WA ?

Posted: Wed Jun 04, 2003 4:49 pm
by Daredevil
Anybody can tell me where my mistake is? This problem REALLY REALLY drives my crazy !!!!!!!!!!!

Here's my code

[c]#include<stdio.h>
int i;
int s,h,u,f,d;
void main(){
while(1){
scanf("%i %i %i %i",&h,&u,&d,&f);
if(h==0){break;}
h*=100;u*=100;d*=100;
for(i=1,s=0,f*=(u/100);;i++){
s+=u;
if(s>h)break;
s-=d;
if(s<0)break;
u-=f;
}
if(s>0) printf("success on day %i\n",i);
else printf("failure on day %i\n",i);
}
}[/c]

Thanks

Posted: Sat Jun 21, 2003 12:20 pm
by shamim
Although the problem says that all input will be integer, but the intermediate values can be floating point.
Since all of your data types are intger, your program will never store any intermediate floating point value.

Try considering all input as floating point value.

Re: 573 The Snail Why WA ?

Posted: Wed Jun 25, 2003 11:22 am
by szymcio2001
In the problem description there is:
If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day.
So you should change:
[c] u-=f; [/c]
to:
[c]
u -= f;
if (u < 0) u = 0;
[/c]

Posted: Thu Jun 26, 2003 5:59 pm
by Daredevil
Thanks to both of you.
I got AC now!!
:D :D :D God bless you :D :D :D

Good.

Posted: Sun Jun 20, 2004 12:10 am
by _.B._
shamin, szymcio2001, keep posting!.

Posted: Wed Nov 24, 2004 7:24 am
by Heartattack!
Yahoo,
You said:
dayclimb = (float)(climb) - (day - 1)*decline;
total += dayclimb;
What if (day-1)*decline<0? It says the snail never climbs a negative distance. So if(dayclimb<0) dayclimb=0;should solve it.
I haven't checked your code thoroughly, but 2 more traps:
1. The snail has to exceed the height of the well, not equal it.
2. The height of the snail has to be less than, not equal to 0.

And searching the thread means clicking the search button next to the faq button on the top of this page. But you knew that probably, seeing you have so many posts. :lol: :lol: