腾讯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");
}
#腾讯##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");
}