0
嗨,我剛開始編程,我想知道是否有人能深入瞭解或者對如何更好地組織代碼或使事情更高效下面的代碼提示:有什麼辦法可以使下面的代碼更有效率?
#include <stdio.h>
#include <math.h>
main()
{
/* Variable Definitions */
int altitude, speed;
float angle, combo, earthRadius, gravityAcceleration, horizontalDistance, pi, radians, time, verticalDistance;
pi = 3.141592653589793238;
/* User Input */
printf("Let's play golf!\n\n");
printf("Enter altitude [m] : ");
scanf("%d", &altitude);
printf("Enter speed [m/s] : ");
scanf("%d", &speed);
printf("Enter angle [degrees]: ");
scanf("%f", &angle);
printf("Enter time [s] : ");
scanf("%f", &time);
printf("\n");
/* Calculations */
radians = angle * (pi/180);
earthRadius = 6.371E6;
gravityAcceleration = 9.8 * pow((earthRadius/(earthRadius + altitude)), 2);
/* Formula Definitions */
horizontalDistance = speed * (cos(radians)) * time;
verticalDistance = (speed * (sin(radians)) * time) - (0.5 * gravityAcceleration * (pow(time, 2)));
/* Output Statements */
printf("Horizontal distance = %.1lf m\n", horizontalDistance);
printf("Vertical distance = %.1lf m\n", verticalDistance);
}
更高效的是什麼意義?該計劃非常簡單,你想在這裏完成什麼? – 2011-01-25 00:26:08
如果您刪除了所有對`scanf`的調用並且不等待用戶輸入,它將運行得更快。 – 2011-01-25 00:26:40