题目的大意是:现在有1...n总共n个不同的数字,从里面任选三个作为三边长,能构成三角形的个数一共有多少个。
解答:设c(x)是最长边为x时的三角形个数,那么要求的答案就等于c(1) + c(2) + ...... + c(x),所以主要需要解决的是c(x),又设三边长分别为x,y,z,由x-y < z < x得到:
1.当y = 1时,z有0个解;
2.当y = 2时,z有1个解;
......
......
3.当y = x - 1时,z有x - 2个解。
另外,里面有x = y的情况要去掉,即x = y 从 x/2 + 1 ~ x - 1, 一共有x - x / 2 - 1个,然后计算的总数再来除以2,因为每种情况都被计算了两遍,比如有y = 2,z = x - 1和y = x - 1,z = 2两种情况。
#include "stdio.h"
#include "string.h"
#include "math.h"
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXM 1
#define MAXN 1
#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b
#define Mem(a,b) memset(a,b,sizeof(a))
int Mod = 1000000007;
double pi = acos(-1.0);
double eps = 1e-6;
typedef struct{
int f,t,w,next;
}Edge;
Edge edge[MAXM];
int head[MAXN];
int kNum;
void addEdge(int f, int t, int w)
{
edge[kNum].f = f;
edge[kNum].t = t;
edge[kNum].w = w;
edge[kNum].next = head[f];
head[f] = kNum ++;
}
long long f[1000005];
void func(){
f[0] = 0;
for(long long i = 1; i <= 1000000; i ++){
f[i] = f[i-1] + ((i - 2) * (i - 1) / 2 - (i - i / 2 - 1) ) / 2;
}
}
int N;
int main()
{
// freopen("d:\\test.txt", "r", stdin);
func();
while(cin>>N){
if( N < 3 )
break;
cout<<f[N]<<endl;
}
return 0;
}