UVA 356 SquarePegs And Round Holes(点到圆心的距离)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=292
题意:
一个直径为2*n-1单位长度的圆被画在了2n*2n的网格棋盘中间.现在要你求出有多少个小格子是被圆包含的,有多少个小格子是被圆相交的?
分析:
我们假设网格棋盘左上角的坐标为(0,0), 那么圆心坐标为(n,n).
那么第一个网格(第0行0列的网格)由 (0,0),(0,1),(1,0),(1,1) 这4个网格点构成. 第i行j列的网格由(i,j),(i+1,j),(i,j+1),(i+1,j+1)这4个网格点构成.
所以如果一个网格被圆完全包围,那么它的4个角上的点距离圆心必然<=半径.
如果一个网格的4个角上的点距离圆心都>=半径,那么该网格必然在圆外.
剩下的情况就是网格与圆相交了.
直接2重循环判断每个网格的情况即可.
AC代码:
#include<cstdio>
#include<cmath>
using namespace std;
int n;
double dist(int i,int j)//(i,j)点到圆心的距离
{
return sqrt((n-i)*(n-i)+(n-j)*(n-j));
}
int main()
{
bool flag=false;
while(scanf("%d",&n)==1)
{
if(flag) printf("\n");
flag=true;
int num_in=0; //在圆内的网格数
int num_cross=0;//与圆相交的网格数
for(int i=0;i<2*n;++i)
for(int j=0;j<2*n;++j)
{
if(dist(i,j)<=n-0.5 && dist(i+1,j)<=n-0.5 && dist(i,j+1)<=n-0.5 && dist(i+1,j+1)<=n-0.5 )
++num_in;
else if(dist(i,j)>=n-0.5 && dist(i+1,j)>=n-0.5 && dist(i,j+1)>=n-0.5 && dist(i+1,j+1)>=n-0.5 )
continue;
else
++num_cross;
}
printf("In the case n = %d, %d cells contain segments of the circle.\n",n,num_cross);
printf("There are %d cells completely contained in the circle.\n",num_in);
}
return 0;
}
分享到:
相关推荐
The round pegs in the square holes. The ones who see things differently. They’re not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or ...
pegs-and-jokers
PEG的DUG 的bug
the player's pegs were in the correct position (small black peg), and how many were the correct colour but in the wrong position (small white peg). From this meager feedback, the player would have ...
PuzzleScript 中的挂钩用于 TI-83+ 图形计算器的游戏 Pegs,移植到浏览器的。 ,还是在PuzzleScript编辑器。 是 Detached Solutions 的 PuzzPack 的一部分,其主要开发人员是 Fred Coughlin。 这个端口是由。 另请...
包括用于完整样式控制的 BEM 样式类。安装将“tags”中的“blockquote.php”复制到Kirby的“site/tags/”文件夹中。用法以下属性可用: blockquote : 你的 blockquote 文本。... The round pegs in the square
太空钉玩转jpeg编码和解码不知道,也许是太空猪和僵尸。
一个集中于宠物的网站 功能全面。 当初做这个网站费了我不少心思,实在没办法,要下载,就得上传。
通过不添加外源激素进行肉欢蓉细胞悬浮培养,考察4种氨基酸对无外源激素悬浮培养肉欢蓉细胞生长和产苯乙醇普(PeGs)的影响。结果表明:苯丙氨酸(Phe)和酪氨酸(Tyr)对PeGs合成的促进作用较大,而色氨酸(Try)和精氨酸(Arg)...
而是将其下载到计算机中,然后使用PDF查看器打开它。 链接应在此处单击。 本书约定: 表1.图标的含义 图标 意义 :green_book: 本节满足的精确参考要求 [图书] 本部分与之相关的精确引用的要求 [已关闭的书] ...
位于苏格兰爱丁堡的地质测试...据表示,PEGS是未来月球与火星太空探险计划的一部份,这些计划包括有人和无人任务。该任务将取得星球表面的地震资料,以便进一步了解星球内部结构。 NASA的测试是在美国亚利桑纳州进行,
Mastermind or Master Mind is a code-breaking game for two players. The modern game with pegs was invented in 1970 by Mordecai Meirowitz
if (pegTop[PEGS-1]==(discs - 1)) { if (moveCount==minMoves) status = "Congratulations!" ; else status = "You did it! Now try again, making only " + minMoves + " moves." ; } return status ; } ...