Captain Hammer - Practice Round China New Grad Test 2014 - 精度控制
2014-03-09 21:39
316 查看
Captain Hammer
题意:简单的应用抛物线相关公式。难度:1星。
从文件中读取数据之后,应用简单的物理数学知识,可以得到结果,再将结果输出到文件。
需要注意:
1)C++的文件操作;
2)输出文件数据的小数保证10^-6位的绝对正确,则输出到10^-7位,PI的精度也需要很高:
3)可能由于double存储小数机制与int不一样,本应相等的两个数之比结果为1.000000000002(之类的),导致asin()返回值非预期,不得已将其直接转为1。此问题待求证!
题解代码:
题意:简单的应用抛物线相关公式。难度: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; }
相关文章推荐
- Practice Round China New Grad Test 2014
- Bad Horse - Practice Round China New Grad Test 2014 - BFS - 二分图判定
- Practice Round China New Grad Test 2014
- Read Phone Number - Round A China New Grad Test 2014 - 循环控制
- Practice Round China New Grad Test 2014
- Practice Round China New Grad Test 2014
- Practice Round China New Grad Test 2014 报告
- Moist - Practice Round China New Grad Test 2014 - 插入排序
- Google China New Grad Test 2014 Round A Problem C
- [Round B] China New Grad Test 2014: Problem E. Ignore all my comments
- Round A China New Grad Test 2014
- Google China New Grad Test 2014 Round A Problem D
- Google China New Grad Test 2014 Round A Problem E
- Google中国2014校园招聘笔试Round A China New Grad Test Problem E. Spaceship Defence
- Google Round B China New Grad Test 2014
- Round B China New Grad Test 2014
- Round A China New Grad Test 2014 Problem A. Read Phone Number
- China New Grad Test 2014 Online Round A
- Round A China New Grad Test 2014 Problem C. Sorting
- Google中国2014校园招聘笔试Round B China New Grad Test Problem E. Ignore all my comments