题意:简单的应用抛物线相关公式。难度:1星。
从文件中读取数据之后,应用简单的物理数学知识,可以得到结果,再将结果输出到文件。
需要注意:
1)C++的文件操作;
2)输出文件数据的小数保证10^-6位的绝对正确,则输出到10^-7位,PI的精度也需要很高:
#define PI 3.14159265359
outputFile.setf(ios::fixed);
outputFile << setprecision(7) << "Case #" << i + 1 << ": " << theta << endl;
3)可能由于double存储小数机制与int不一样,本应相等的两个数之比结果为1.000000000002(之类的),导致asin()返回值非预期,不得已将其直接转为1。此问题待求证!
题解代码:
/*
* Captain Hammer - Practice Round China New Grad Test 2014
*
*
*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define GRAVITY 9.8
#define PI 3.14159265359
int main()
{
int T, V, D;
double theta;
ifstream inputFile;
ofstream outputFile;
inputFile.open("E:\\C++\\GCJ\\China New Grad Test 2014\\Practice Round\\Captain Hammer\\B-small-practice.in");
outputFile.open("E:\\C++\\GCJ\\China New Grad Test 2014\\Practice Round\\Captain Hammer\\B-small-practice.out");
inputFile >> T;
for (int i = 0; i < T; i++)
{
inputFile >> V >> D;
double tmp = (double)D * GRAVITY / (V * V);
theta = tmp >= 1 ? 45 : 0.5 * asin(tmp) / PI * 180;
cout.setf(ios::fixed);
cout << setprecision(7) << "Case #" << i + 1 << ": " << theta << endl;
outputFile.setf(ios::fixed);
outputFile << setprecision(7) << "Case #" << i + 1 << ": " << theta << endl;
}
inputFile.close();
outputFile.close();
system("pause");
return 0;
}