uva11401(数学基础题)

  题目的大意是:现在有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值