符号三角形

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. 实际案例告诉你为什么Oracle不建议使用varchar2来存时间数据

    问题现象2015年9月客户系统中一条高逻辑读的SQL语句,在业务高峰期执行频率较高,导致系统逻辑读居高不下,同时带高了系统CPU,SQL语句主体部分如下 SELECT /* ^^*/ COUNT(DI ...

  2. Make a Person-freecodecamp算法题目

    Make a Person 1.要求 用下面给定的方法构造一个对象:方法有 getFirstName(), getLastName(), getFullName(), setFirstName(fir ...

  3. spring-bean(注解方式-管理及依赖注入)

    Bean管理(注解方式) 1.添加注解的依赖包:Spring-aop.jar 2.配置spring的XML文件的引入(查官方源码) 3.开启注解的扫描 <context:component-sc ...

  4. Servlet学习笔记02——什么是http协议?

    1.http协议 (了解) (1)什么是http协议? 是一种网络应用层协议,规定了浏览器与web服务器之间 如何通信以及相应的数据包的结构. 注: a.tcp/ip: 网络层协议,可以保证数据可靠的 ...

  5. layer 的功能

    1.layer.alert() layer.alert('',{ title: "<div style='color:red;margin-left:20px;font-size:20 ...

  6. SpringMVC注解@RequestParam解析

    1.可以对传入参数指定参数名 1 @RequestParam String inputStr 2 // 下面的对传入参数指定为param,如果前端不传param参数名,会报错 3 @RequestPa ...

  7. ES6笔记01-声明变量

    ES6只有六种声明变量的方法:var命令和function命令,let和const命令,import命令和class命令.所以,ES6一共有6种声明变量的方法. const声明一个只读的常量.一旦声明 ...

  8. (三)Swagger配置多项目共用

    重构了多个项目后,在联调接口时,查看api会发现Swagger在几个项目可用,有几个不可用,配置都一样,扫描也充分,那问题出在哪里呢?先仔细找了下Docket的源码,发现有这么个方法: /** * P ...

  9. CSS选取指定位置标签first-child、last-child、nth-child

    1.first-child 选择列表中的第一个标签. 2.last-child 选择列表中的最后一个标签 3.nth-child(n) 选择列表中的第n个标签 4.nth-child(2n) 选择列表 ...

  10. POJ:2456-Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18313 Accepted: 8716 Desc ...