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 */