POJ_1005

//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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值