符号三角形

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2512 2515 2509 2517 2514 
 

这题还比较好吧,开始写了代码发现一定会超时,后来看了别人的解法再想想,可以打表解决,小菜的dfs一般,解24时要好几分钟...

还有题目加减号的情况和异或一样,于是用01表示方便计算。

#include<stdio.h>
#include<string.h>
int n,cnt,m;
int c[];
int judge(int c0[])
{
int cc=;
int s[];
for(int i=;i<n;i++){
s[i]=c0[i];
if(c0[i]) cc++;
}
for(int i=;i<n;i++){
for(int j=;j<n;j++) s[j]=c0[j];
for(int j=;j<n-i-;j++)
if(c0[j]=s[j]^s[j+]) cc++;
}
if(cc==m) return ;
return ;
}
void dfs(int id)
{
if(id==n){
cnt+=judge(c);return;
}
c[id]=;
dfs(id+);
c[id]=;
dfs(id+);
return;
}
int main(void)
{
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
memset(c,,sizeof(c));
m=(n+)*n/;
if(m%){ //小剪枝
printf("%d %d\n",n,);
continue;
}
m/=;
cnt=;
dfs();
printf("%d %d\n",n,cnt);
}
return ;
}
/* */

求得解后打表

 #include<stdio.h>
int ans[]={,,,,,,,,,,,,,,,,,,,,,,,,};
int main(void)
{
int n;
while(scanf("%d",&n),n)
{
printf("%d %d\n",n,ans[n]);
}
return ;
}

hdu 2510 符号三角形 (DFS+打表)的更多相关文章

  1. HDU 2510 - 符号三角形

    DFS后打表 #include <iostream> using namespace std; ,,,,,,,,,,,,,,,,,,,,,,,,}; int main() { int n; ...

  2. HDU 2563 统计问题 (DFS + 打表)

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. ACM: HDU 2563 统计问题-DFS+打表

    HDU 2563 统计问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u HDU 2 ...

  4. 【HDOJ】2510 符号三角形

    暴力打表. #include <cstdio> ]={,,,,,,,,,,,,,,,,,,,,,,,,}; int main() { while (scanf("%d" ...

  5. 符号三角形(hdu 2510 搜索+打表)

    符号三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. HDU 2586 How far away(dfs+邻接表)

    How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...

  7. 符号三角形_hdu_2510(深搜).java

    http://acm.hdu.edu.cn/showproblem.php?pid=2510 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  8. 符号三角形——F

    F. 符号三角形 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format:      Java class name: 符号 ...

  9. code vs 3376 符号三角形

    3376 符号三角形  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 如下图是由14个“+”和14个“-”组 ...

随机推荐

  1. 转载:字符串HASH

    转载自:Slager_Z 字符串Hash总结 Hash是什么意思呢?某度翻译告诉我们: hash 英[hæʃ] 美[hæʃ]n. 剁碎的食物; #号; 蔬菜肉丁;vt. 把…弄乱; 切碎; 反复推敲; ...

  2. HTML5一些标签和属性

    <bdo> 元素 可以覆盖默认文本的方向  根据dir 属性来控制文字的排序方向            属性:dir="rtl"                     ...

  3. hibernate系列之四

    数据库中表之间的关系: 一对一.一对多.多对多 一对多的建表原则:在多的一方创建外键指向一的一方的主键: 多对多的建表原则:创建一个中间表,中间表中至少有两个字段作为外键分别指向多对多双方的主键: 一 ...

  4. LeetCode969. 煎饼排序

    问题:969. 煎饼排序 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零次或多次煎饼翻转(按顺序一次接 ...

  5. (转)数据库老兵:NewSQL才是未来

    编者按:在数据库技术领域,Michael Stonebraker几乎是无人不知无人不晓的人物.现年70岁的Stonebraker不仅是Ingres和PostgreSQL的创始人,同时在Informix ...

  6. js延迟加载的方式有哪些?

    共有:defer和async.动态创建DOM方式(用得最多).按需异步载入js defer属性:(页面load后执行) HTML 4.01 为 <script>标签定义了 defer属性. ...

  7. 使用windows api安装windows服务程序(C#)

    3个步骤: 1.安装器代码编写 2.安装器工具类编写 1)安装.启动服务) 2)卸载服务 3.windows服务程序编写(参考:多线程.方便扩展的Windows服务程序框架) 4.代码下载,在文末(注 ...

  8. 在intellij idea 里来回跳转查询方法

    在intellij idea 里来回跳转查询方法,在不知道快捷键的时候真是抓狂. 看到key map中的快捷键 后退back  ctrl +alt + ← 或者button4 click 前进forw ...

  9. POJ:3061-Subsequence(尺取法模板详解)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...

  10. Black Box POJ1442

    Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...