Triangles

Problem description
You will be given N points on a circle. You must write a program to determine how many distinct equilateral triangles can be constructed using the given points as vertices.
The gure below illustrates an example: (a) shows a set of points, determined by the lengths of the circular arcs that have adjacent points as extremes; and (b) shows the two triangles which can be built with these points.
Input
The rst line of the input contains an integer N, the number of points given. The second line contains N integers Xi, representing the lengths of the circular arcs between two consecutive points in the circle: for 1<=i<=(N-1), Xi represents the length of the arc between between points i and i + 1; XN represents the length of the arc between points N and 1.
Output
Your program must output a single line, containing a single integer, the number of distinct equilateral triangles that can be constructed using the given points as vertices.
Sample Input
8
4 2 4 2 2 6 2 2
6
3 4 2 1 5 3
Sample Output
2
1
//题意:已知在一圆上有N个点,而这N个点的相邻点的距离已知,问从这N个点能找出多少个等边三角形。
//题解:只要找到三个点是它们的弧长相等就OK了。
//标程:
#include 
#include 
#include 
using namespace std;
int sum[1000010], a[1000010];
set s;
int main()
{
//    freopen("a.txt","r",stdin);
    int n, i;
    while(scanf("%d",&n)!=EOF)
    {
        s.clear();
        sum[0] = 0;
        for(i = 1; i <= n; ++ i)
        {
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + a[i];
            s.insert(sum[i]);
        }
        int temp = sum[n]/3, cnt = 0;
        for(i = 1; i <= n - 2; ++ i)
            if(s.count(sum[i])==1 && s.count(sum[i]+temp)==1 && s.count(sum[i]+2*temp)==1)
                cnt ++;
        printf("%d\n",cnt);
    }
    return 0;
}

你可能感兴趣的:(水题)