UVa 356 - Square Pegs And Round Holes
來源:程序員人生 發布時間:2014-12-22 08:27:57 閱讀次數:2519次
題目:在1個2n*2n的網格中間畫1個直徑為2n⑴的圓,問圓內部的格子和和圓相交的格子個數。
分析:計算幾何。分別計算出圓和每一個整數橫坐標的交點(xi,yi)。
ceil(yi)- ceil(yi⑴)即為每列的相交格子個數,floor(yi)即為每列圓內格子個數。
說明:注意精度(⊙_⊙)。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int n,in,on,count = 0;
while (cin >> n) {
in = 0,on = 0;
double r,l = n-0.5;
for (int i = 1 ; i < n ; ++ i) {
r = sqrt((n-0.5)*(n-0.5)-i*i);
on += ((int)(l+1⑴e⑴2) - (int)(r+1⑴e⑴2)+1)<<2;
in += ((int)(r+1e⑴2))<<2;
l = r;
}
on += ((int)(l+1⑴e⑴0))<<2;
if (count ++) printf("
");
printf("In the case n = %d, %d cells contain segments of the circle.
",n,on);
printf("There are %d cells completely contained in the circle.
",in);
}
return 0;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈