hdu2510 符号三角形(dfs+打表) 解题报告
2015-07-30 22:44
281 查看
Problem Description
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。 n=7时的1个符号三角形如下:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
Input
每行1个正整数n <=24,n=0退出.
Output
n和符号三角形的个数.
Sample Input
Sample Output
暴力思路:题目要求下一行由上一行两个得,所以,知道下一个和上一个得左边,也可以得到上一行得右边。
代码如下:
提交代码:
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。 n=7时的1个符号三角形如下:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
Input
每行1个正整数n <=24,n=0退出.
Output
n和符号三角形的个数.
Sample Input
15 16<span style="font-family: 'Courier New', Courier, monospace; font-size: 14px; white-space: pre-wrap; background-color: rgb(240, 240, 240);">19</span>200
Sample Output
15 1896 16 5160 19 32757 20 59984
暴力思路:题目要求下一行由上一行两个得,所以,知道下一个和上一个得左边,也可以得到上一行得右边。
代码如下:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <iostream> #include <vector> #include <map> using namespace std; const int maxn=30; int f[maxn][maxn]; int res[maxn]; void dfs(int t,int s)//层数,和 { int i,j,ans; for(i=0; i<2; i++)//枚举第一个数 { f[t][0]=i; ans=i; for(j=1; j<t; j++) { f[t][j]=f[t][j-1]^f[t-1][j-1];//因为题目要求 ans+=f[t][j]; } if((1+t)*t/2==(ans+s)*2)res[t]++; if(t==24)continue; dfs(t+1,s+ans); } } void init() { memset(f,0,sizeof(f)); memset(res,0,sizeof(res)); dfs(1,0); } int main() { init(); for(int i=1; i<=24; i++) { if(i==1)printf("%d",res[i]); else if(i==24)printf(",%d\n",res[i]); else printf(",%d",res[i]); } } //打表得0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757,59984,0,0,431095,822229
提交代码:
#include <cstdio> using namespace std; const int maxn=30; int ans[maxn]={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() { int n; while(scanf("%d",&n)!=EOF&&n!=0) { printf("%d %d\n",n,ans[n-1]); } return 0; }
相关文章推荐
- Json对象与Json字符串互转(4种转换方式)
- Spring multipart file上传
- OSPF的末节区域和完全末节区域
- OSPF的末节区域和完全末节区域
- Scala学习第四天:For与Function进阶实战、Lazy的使用
- 初学者之php基础(三)
- 前后端分离
- 函数指针(——C primer 第13章)
- VC学习笔记8INI文件操作
- Algorithms—205.Isomorphic Strings
- 数据分析与数据仓库建模
- cvRound, cvFloor, cvCeil
- 第五篇:在opencv中iplimage转化成Mat
- 三、快速学习创建版本库、管理修改、查看状态、分支、标签、变基、克隆远程版本库
- 一个demo
- bzoj1004【hnoi2008】Cards
- SSH学习六 Hibernate 一对多 多对一的双向映射
- HDU 5335 Walk Out BFS + 贪心 2015 Multi-University Training Contest 4 1009
- SDWebImage使用
- git分支管理