573 - The Snail
Moderator: Board moderators
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;
}
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;
}
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 */
#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 */
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
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
-
- 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:
[c] u-=f; [/c]
to:
[c]
u -= f;
if (u < 0) u = 0;
[/c]
So you should change:If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day.
[c] u-=f; [/c]
to:
[c]
u -= f;
if (u < 0) u = 0;
[/c]
-
- New poster
- Posts: 45
- Joined: Fri Jan 16, 2004 7:02 pm
- Location: CSE::BUET
- Contact:
Yahoo,
You said:
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.
You said:
What if (day-1)*decline<0? It says the snail never climbs a negative distance. So if(dayclimb<0) dayclimb=0;should solve it.dayclimb = (float)(climb) - (day - 1)*decline;
total += dayclimb;
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!!!!