腾讯9.18笔试

第三题,求合法三角形个数
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{

int t;
cin >> t;
vector<int> res;//记录结果
for (int i = 0; i < t; i++)
{
vector<int> a;
int k;
cin >> k;
for (int j = 0; j < k; j++)
{
int tmpnumber;
cin >> tmpnumber;
a.push_back(tmpnumber);
}
sort(a.begin(), a.end());

int len = a.size();

int pos1 = 0, pos2 = 1, pos3 = 3;
int cnt=0;//记录个数

while (pos3 < len)
{
if (a[pos1] + a[pos2] > a[pos3])
{

cnt++;
}

if (pos1 + 1 < pos2)
{
pos1++;
}
else
if (pos2 + 1 < pos3)
{
pos2++;
}
else
{
pos3++;
}

}
res.push_back(cnt);
}

for (auto i = res.begin(); i != res.end(); i++)
{
cout << *i << endl;
}

system("pause");
}

#腾讯#
全部评论

相关推荐

lllllkin:感觉可以精简到一页简历,有些排版感觉不是必须的。 时间线越早的,你自己越熟悉的放前面。描述可以更精简些,一些问题解决感觉可以不用写具体技术栈,卖个关子,等面试官问。
点赞 评论 收藏
分享
哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务