刚学没多久,不懂是什么原因。
#include
#include
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char argv[]) {
int d,g=32,t,sum=0;
for(t=0;t<11;t++){
d=1g*pow(t,2)/2;
sum+=d;
cout<<"time is:"<<t<<endl;
cout<<"Distance in the Current Time Interval (ft):"<<d<<endl;
cout<<"Total Distance (ft):"<<sum<<endl;
}
return 0;
}
因为pow运算结果是double,而你定义的d和sum都是int,所以会有精度损失。改成double就行了
但是不同编译器也可能不同,vs高版本也许在这个测试用例中不会有这样的问题
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char argv[])
{
double d, g = 32, t, sum = 0;
for (t = 0; t < 11; t++) {
d = g*pow(t, 2) / 2;
sum += d;
cout << "time is:" << t << endl;
cout << "Distance in the Current Time Interval (ft):" << d << endl;
cout << "Total Distance (ft):" << sum << endl;
}
return 0;
}
效果: