## 573 - The Snail

Moderator: Board moderators

zakaria
New poster
Posts: 15
Joined: Thu Jun 27, 2002 11:34 pm

### 573 - The Snail

#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;
}
Kamp
New poster
Posts: 18
Joined: Tue Mar 05, 2002 2:00 am
Location: Poland (3-city)
Contact:
Pls specify the number of this problem

Good Luck
zakaria
New poster
Posts: 15
Joined: Thu Jun 27, 2002 11:34 pm
The number of this problem is 573.
Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:
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".
Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:
sorry 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
zakaria
New poster
Posts: 15
Joined: Thu Jun 27, 2002 11:34 pm
Yes it got accepted.
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

### 573

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 */
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Please searched the forum before posting question.
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am
Daredevil
New poster
Posts: 19
Joined: Tue Apr 01, 2003 7:47 am
Location: Earth

### 573 The Snail Why WA ?

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
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA
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.
szymcio2001
New poster
Posts: 38
Joined: Mon Dec 09, 2002 1:53 pm
Location: Poznan, Poland

### Re: 573 The Snail Why WA ?

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]
Daredevil
New poster
Posts: 19
Joined: Tue Apr 01, 2003 7:47 am
Location: Earth
Thanks to both of you.
I got AC now!!
God bless you
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

### Good.

shamin, szymcio2001, keep posting!.
_.
Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:
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.
We will, We will BREAK LOOP!!!!