算法——求圆上点能构成三角形的数目

输入:n——点的个数,a:double的点的角度。

 

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <vector>
 5 #include <array>
 6 #include <list>
 7 #include <string>
 8 
 9 
10 using namespace std;
11 
12 bool compare(vector<double> b, size_t slow, size_t fast, size_t length)
13 {
14     if (fast < length)
15     {
16         return (b[fast] - b[slow]) < 180;
17     }
18     else
19     {
20         return (360 - b[slow] + b[fast]) < 180;
21     }
22 }
23 
24 long func2(vector<double>& a)
25 {
26     size_t length = a.size();
27     long ret = 0;
28     size_t slow = 0, fast = 1;
29     vector<double> b(a);
30     b.insert(b.end(), a.begin(), a.end());
31     for (slow = 0; slow < length; ++slow)
32     {
33         while (compare(b, slow, fast, length))
34         {
35             ++fast;
36         }
37         fast--;
38         ret += (fast / 2 - slow / 2 - 1)*(fast - slow + 1) + 1;
39         
40     }
41     return ret;
42 }
43 
44 
45 int main()
46 {
47 
48     int n;
49     vector<double> a;
50     scanf("%d", &n);
51     a.resize(n);
52     for (int i = 0; i < n; ++i)
53     {
54         scanf("%lf", &a[i]);
55     }
56 
57     long ret = func2(a);
58     printf("%d\n", ret);
59 
60     return 0;
61 }
复制代码

 

posted @   鸭子船长  阅读(107)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2017-02-08 【Android Studio】为Android Studio设置HTTP代理
2017-02-08 android-problem——remount of /system failed: Read-only file system
2017-02-08 Android——坐标系及转化
点击右上角即可分享
微信分享提示