码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 356 - Square Pegs And Round Holes

时间:2019-08-25 01:01:18      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:let   ase   include   思路   complete   round   contain   ++   ret   

题目:在一个2n*2n的网格中间画一个直径为2n-1的圆,问圆内部的格子以及和圆相交的格子个数。

思路:只要考虑1 / 4圆的点就行,用点到原点距离与半径比较,当格子左下方和右上方都在格子里时,格子在圆内部。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int sqr(int n, int m)
 4 {
 5     return m * m + n * n;
 6 }
 7 int main()
 8 {
 9     int n;
10     int cnt = 0;
11     while(cin >> n)
12     {
13         if(cnt++) cout << endl;
14         double r = (n - 0.5) *(n - 0.5);
15         int in = 0;
16         int side = 0;
17         for(int i = 0;i < n;i++)
18         {
19             for(int j = 0;j < n;j++)
20             {
21                 if(sqr(i , j) < r && sqr(i + 1, j + 1) < r ) in ++;
22                 else if(sqr(i , j) < r && sqr(i + 1, j + 1) > r) side++;
23             }
24         }
25         printf("In the case n = %d, %d cells contain segments of the circle.\n",n,side*4);
26         printf("There are %d cells completely contained in the circle.\n",in*4);
27     }
28     return 0;
29 }

 

UVA 356 - Square Pegs And Round Holes

标签:let   ase   include   思路   complete   round   contain   ++   ret   

原文地址:https://www.cnblogs.com/Carered/p/11406612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!