programming-challenges Dermuba Triangle (111207) 题解

很考验细心的一道题。把各种情况分析清楚了,就不难写出对的程序了。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

struct Point {
	int x, y; 
	Point(int ax, int ay) : x(ax), y(ay) {}
};

int getRow(long long index) {
	long long r = sqrt(index); 
	if (r * r == index) {
		return r; 
	}
	else return r + 1; 
}

Point getPoint(long long index) {
	int row = getRow(index);

	int lastRow = row - 1; 
	int col = index - lastRow * lastRow;

	return Point(row, col - row);
}

int main() {
	long long e1, e2; 

	double a = sqrt(3) / 3 / 2; 
	double b = sqrt(3) / 3; 

	while (cin >> e1) {
		cin >> e2; 

		if (e1 > e2) {
			long long t = e2; 
			e2 = e1; 
			e1 = t; 
		}

		Point p1 = getPoint(e1 + 1);
		Point p2 = getPoint(e2 + 1); 

		double l = ((double)llabs(p1.y - p2.y)) / 2;

		double h; 
		double h1 = 0, h2 = 0, h3 = 0;
		if (p2.x > p1.x) {
			if ((p1.x + p1.y) % 2 == 0) {
				h1 = b;
			}
			else {
				h1 = a;
			}

			if ((p2.x + p2.y) % 2 == 0) {
				h2 = a;
			}
			else {
				h2 = b;
			}

			h3 = (p2.x - p1.x - 1) * sqrt(3) / 2;
			h = h1 + h2 + h3;
		} 
		else {
			if (abs(p1.y % 2) == abs(p2.y % 2)) {
				h = 0; 
			}
			else {
				h = sqrt(3) / 6; 
			}
		}

		printf("%.3f\n", sqrt((l * l + h * h)));
	}

	return 0; 
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值