HDU 2510 符号三角形

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2510

符号三角形

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1116 Accepted Submission(s): 578

Problem Description

符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。 n=7时的1个符号三角形如下:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -

+


Input

每行1个正整数n <=24,n=0退出.

Output

n和符号三角形的个数.

Sample Input

15
16
19
20
0

Sample Output

15 1896
16 5160
19 32757
20 59984

Source
ECJTU 2008 Autumn Contest

Recommend
lcy

大意——符号三角形的第1行有n个由“+”和“-”组成的符号,以后每行符号比上行少1个,2个同号下面是“+”,2个异号下面是“-”。问:计算有多少个不同的符号三角形,使其所含“+”和“-”的个数相同。输出n及个数。

思路——因为n最大为24,所以写一个暴力程序打表即可。而且要使得+和-相等,所以满足n*(n+1)/2等于奇数的n输出为0。然后把+看做0,-看做1,这样就可以通过异或运算求出。

复杂度分析——时间复杂度:O(1),空间复杂度:O(n)

附上AC代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double pi = acos(-1.0);
const double e = exp(1.0);
const int num[25] = {0, 0, 0, 4, 6, 0, 0, 12, 40, 0, 0, 171,
					410, 0, 0, 1896, 5160, 0, 0, 32757, 59984,
					0, 0, 431095, 822229};

int main()
{
	ios::sync_with_stdio(false);
	int n;
	while (scanf("%d", &n)==1 && n!=0)
	{
		printf("%d %d\n", n, num[n]);
	}
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值