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


    输入: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 }
  • 相关阅读:
    将一个单向链表逆序
    高精度加法
    从反汇编的角度看引用和指针的区别
    冒泡排序
    josephus(约瑟夫)问题
    获取每个进程可打开的最大文件数量
    AIX免费终端的获取
    [转] Linux应用层的定时器Timer
    POD(plain old data)
    char与wchar_t的区别
  • 原文地址:https://www.cnblogs.com/zl1991/p/7577041.html
Copyright © 2020-2023  润新知