573 - The Snail

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

573 - The Snail

Post by zakaria » Thu Jun 27, 2002 11:39 pm

#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:

Post by Kamp » Fri Jun 28, 2002 12:37 am

Pls specify the number of this problem :)

Good Luck :D

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

Post by zakaria » Fri Jun 28, 2002 10:19 am

Thanks for reply.
The number of this problem is 573.

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Fri Jun 28, 2002 11:06 am

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:

Post by Picard » Fri Jun 28, 2002 11:12 am

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

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

Post by zakaria » Sat Jun 29, 2002 5:33 am

Yes it got accepted.
Thanks for your reply.

User avatar
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

573

Post by yahoo » Wed Jul 10, 2002 3:40 pm

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:

Post by 10153EN » Wed Jul 10, 2002 3:51 pm

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.

User avatar
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

Post by yahoo » Sat Jul 13, 2002 8:06 am

Woubld you please explain what do you mean by searching thread about this problem? As a new programmer it is unknown to me.

Daredevil
New poster
Posts: 19
Joined: Tue Apr 01, 2003 7:47 am
Location: Earth

573 The Snail Why WA ?

Post by Daredevil » Wed Jun 04, 2003 4:49 pm

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

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Sat Jun 21, 2003 12:20 pm

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.

User avatar
szymcio2001
New poster
Posts: 38
Joined: Mon Dec 09, 2002 1:53 pm
Location: Poznan, Poland

Re: 573 The Snail Why WA ?

Post by szymcio2001 » Wed Jun 25, 2003 11:22 am

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

Post by Daredevil » Thu Jun 26, 2003 5:59 pm

Thanks to both of you.
I got AC now!!
:D :D :D God bless you :D :D :D

User avatar
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Good.

Post by _.B._ » Sun Jun 20, 2004 12:10 am

shamin, szymcio2001, keep posting!.
_.

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

Post by Heartattack! » Wed Nov 24, 2004 7:24 am

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:
We will, We will BREAK LOOP!!!!

Post Reply

Return to “Volume 5 (500-599)”