UVA 356 - Square Pegs And Round Holes

题目:在一个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 }

 

转载于:https://www.cnblogs.com/Carered/p/11406612.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值