我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年5月23日 星期六

[UVa] 10233 - Dermuba Triangle

題目網址: http://goo.gl/sx05kx

題意:


解 法: 找出所求正方形為第幾層,設第 0 個三角形上方頂點為座標原點,配合三角形重心的性質,就可以找出三角形重心的座標。

TAG: Math,

注意:

程式碼:
/**
* Tittle: 10233 - Dermuba Triangle
* Author: Cheng-Shih, Wong
* Date: 2015/05/22
*/
// include files
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
// definitions
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i )
#define clr(x,v) memset( x, v, sizeof(x) )
typedef long long ll;
// declarations
ll a, b;
ll la, lb;
double ax, ay, bx, by;
// functions
// main function
int main( void )
{
double r3d2 = sqrt(3)/2.0;
// input
while( scanf( "%lld%lld", &a, &b )==2 ) {
// solve
++a; ++b;
la = sqrt(a);
lb = sqrt(b);
if( la*la < a ) ++la;
if( lb*lb < b ) ++lb;
a -= (la-1)*(la-1);
b -= (lb-1)*(lb-1);
ay = ((la-1) + ((a&1LL) ? 2:1)/3.0)*r3d2;
by = ((lb-1) + ((b&1LL) ? 2:1)/3.0)*r3d2;
ax = 0.5*(a-la);
bx = 0.5*(b-lb);
double dx = ax-bx;
double dy = ay-by;
// output
printf( "%.3lf\n", sqrt( dx*dx+dy*dy ) );
}
return 0;
}

沒有留言:

張貼留言

任何意見都樂意傾聽