POJ_1005

来源:互联网 发布:乐高编程机器人多少钱 编辑:程序博客网 时间:2024/05/16 15:34
//364K  16MS#include <cstdio>#include <cmath>const float PI = atan(1.0f) * 4;float round(int j) {  return sqrt(j * 50 * 2 / PI);}float dis(float x, float y) {  return sqrt(x * x + y * y);}int main() {  int n;  scanf("%d", &n);  for (int i = 1; i <= n; ++i) {    float x, y;    scanf("%f%f", &x, &y);    int j = 1;    for ( ; round(j) < dis(x, y); ++j) {}    printf("Property %d: This property will begin eroding in year %d.\n", i, j);  }  printf("END OF OUTPUT.");  return 0;}


notes:

1. c++中的PI表示:const double PI = std::atan(1.0) * 4; c++ 11就是这样定义的PI,或者是直接 #define PI 3.141592654
2. <cmath>中的一些数学函数,atan, sqrt.

optimize:

1. 代码之所以跑到了16MS的龟速,主要原因是每个Case都从1Year开始迭代计算了Eroding半径,增加了时间复杂度并且每次迭代还有round和dis两个函数调用,开销也很大。
2. 优化的方法是:计算以给定点(x, y)到(0, 0)的距离为半径的半圆面积,即Eroding到这里时候的侵蚀面积,然后除以50/Year,上取整。

代码转自http://blog.csdn.net/lyy289065406/article/details/6642579

//Memory Time//260K   0MS #include<iostream>using namespace std;const double pi=3.141592654;int main(void){int test;cin>>test;for(int t=1;t<=test;t++){double x,y;cin>>x>>y;double Area=pi*(x*x+y*y);int RestYear=(int)(Area/100.0+1.0);cout<<"Property "<<t<<": This property will begin eroding in year "<<RestYear<<'.'<<endl;}cout<<"END OF OUTPUT."<<endl;return 0;}



原创粉丝点击