Train A, traveling 70 miles per hour (mph), leaves Westford heading toward Eastford, 260 miles away. At the same time Train B, traveling 60 mph, leaves Eastford heading toward Westford. When do the two trains meet? How far from each city do they meet?
#include <cstdio>
#define EASTFORD 260
int main() {
int trainA=0,trainB=EASTFORD,hours=0;
while(trainA!=trainB){
trainA+=70;
trainB-=60;
hours++;
}
printf("They meet after %d hours.\n",hours);
printf("Train A will be %dmi away from Westford, and %dmi on the way to Eastford,\n",trainA,EASTFORD-trainA);
printf("Train B will be %dmi away from Eastford, and %dmi to Westford.\n",EASTFORD-trainB,trainB);
return 0;
}
Haha, right, so I'll ask seriel if she can fix solution (modify) using only integer variables. In case when train B traveling with 65mph and if you can't change "while" condition.
#include <cstdio>
#define EASTFORD 260
int main() {
int trainA=0,trainB=EASTFORD,seconds=0;
int vA = 70;
int vB = 65;
while(trainA!=trainB){
seconds = trainB * 3600 / (vA + vB);
trainA = trainB = vA * seconds / 3600;
}
printf("They meet after %d hours %d minutes and %d seconds.\n",seconds/3600,seconds/60%60,seconds%60);
printf("Train A will be %dmi away from Westford, and %dmi on the way to Eastford,\n",trainA,EASTFORD-trainA);
printf("Train B will be %dmi away from Eastford, and %dmi to Westford.\n",EASTFORD-trainA,trainA);
return 0;
}