CSU1011: Counting Pixels
Description
Did you know that if you draw a circle that fills the screen on your 1080p high definition display, almost a million pixels are lit? That's a lot of pixels! But do you know exactly how many pixels are lit? Let's find out!
Assume that our display is set on a Cartesian grid where every pixel is a perfect unit square. For example, one pixel occupies the area of a square with corners (0,0) and (1,1). A circle can be drawn by specifying its center in grid coordinates and its radius. On our display, a pixel is lit if any part of it is covered by the circle being drawn; pixels whose edge or corner are just touched by the circle, however, are not lit.
Your job is to compute the exact number of pixels that are lit when a circle with a given position and radius is drawn.
Input
The input consists of several test cases, each on a separate line. Each test case consists of three integers, x,y, and r(1≤x,y,r≤1,000,000), specifying respectively the center (x,y) and radius of the circle drawn. Input is followed by a single line with x = y = r = 0, which should not be processed.
Output
For each test case, output on a single line the number of pixels that are lit when the specified circle is drawn. Assume that the entire circle will fit within the area of the display.
Sample Input
1 1 1
5 2 5
0 0 0
Sample Output
4
88 题意:给定圆心和半径,要你找这个圆覆盖了多少的矩形。由于圆是中心对称,所以考虑四分之一的圆。
那么怎么想?考虑右上方的四分之圆,如果一个一个矩形被覆盖,那么这个矩形的左下角的点到圆心的距离一定小于半径,可以自己画下图理解,如果这个矩形在圆内,那么这个矩形以下的一列都会被圆覆盖,所以我们考虑离圆心最远
的每个矩形,不断的向右向下走,直到它运动到圆心的水平线下。最后乘以4就是答案,不懂可以看代码和画图理解一下,应该不难。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
const int maxn=1005;
typedef long long LL;
int x,y,r;
int main()
{
while(scanf("%d %d %d",&x,&y,&r)!=EOF)
{
if(x==0&&y==0&&r==0)
return 0;
else
{
LL ans=0;
int i=r-1,j=0;
LL temp=r*r;
while(j<r)
{
if(i*i+j*j<temp)
ans+=(i+1);
else
{
i--;
continue;
}
j++;
}
printf("%lld\n",ans*4);
}
}
return 0;
} /**********************************************************************
Problem: 1011
User: therang
Language: C++
Result: AC
Time:28 ms
Memory:2024 kb
**********************************************************************/
最新文章
- pagerank
- [Android Pro] Normal Permissions
- 精确计算TFS中新增以及更改的代码行数
- NOIP200003方格取数
- 此博客记录我的进阶之路(PHP、C、Python、Erlang)
- C++的类和对象
- LeetCode之Maximum Product Subarray
- cura-engine学习(3)
- 20. Valid Parentheses【leetcode】
- 如何在Win7安装U盘中加入USB3.0驱动的支持
- VMware Workstation 10序列号:
- 集合List的排序
- jmeter测试mysql遇到的问题
- NOIP复习篇
- LeeCX - 开源后台管理系统简单介绍
- 如何清除浮动塌陷? float:left 塌陷
- 【062有新题】OCP 12c 062出现大量之前没有的新考题-16
- SpringBoot整合Swagger-ui
- C#或者.NET下的强制垃圾回收办法
- JavaScript 开闭原则OCP