计算输入序列能组成三角形的个数

时间限制 1000 ms 内存限制 65536 KB

题目描述

Amy learned equilateral triangle today and was interested in it very much. After school, she took out her toys as usual and surprisingly found so many sticks! As the plot goes, she started to count how many different equilateral triangles they could form. However, there're countless sticks and it was not easy for such a little girl to finish this task. Help her!
An equilateral triangle is such a triangle, that the length of its three sides are equal. Two equilateral triangle are different if and only if they have different lengths of sides. Amy only uses one stick to form one side of a triangle.

 

输入格式

The input contains several cases. An integer T(T100) will exist in the first line of input, indicating the number of test cases.
Each test case begins with the number of sticks N(N100). The following line contains N numbers length[i]

(1length[i]100), indicating all the lengths of sticks.

输出格式

Output the answer for each test case in respective lines.
 

输入样例

2
3
2 3 1
4
3 3 3 3

输出样例

0
1

一个测试通过代码:

#include <iostream>
#include <map>
using namespace std;

int solution(int *org, int len)
{
    int res = 0;
    map<int, int> data;
    for (int i = 0; i < len; ++i) {
        data[org[i]]++;
    }
    map<int, int>::iterator itr = data.begin();
    while (itr != data.end()) {
        if (itr->second >= 3)
            ++res;
        ++itr;
    }
    return res;
}

int main()
{
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        int n;
        cin >> n;
        int *data = new int[n];
        for (int j = 0; j < n; ++j) {
            cin >> data[j];
        }
        cout << solution(data, n) << endl;
        delete [] data;
    }
    return 0;
}

转载于:https://www.cnblogs.com/candycloud/p/3591788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值