符号三角形

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. linux服务器安装nginx及使用

    Nginx在个人的使用之后,感觉非常的方便,所以在这里给出自己安装配置方案.它是一款高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.负载均衡是个不错的选择. ...

  2. 总结JavaScript常用数组操作方法,包含ES6方法

    一.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2,3]; var arr2 = [4,5]; ...

  3. ES5 与 ES6六大不同

    1.类Class 2.模块Module 导出变量 导出函数 导入 3.箭头函数 4.不再支持Mixins. 5.ES6不再支持自动绑定.

  4. npm run build根据不同参数打包不同环境url

    config文件夹下 dev.env.js中修改代码 'use strict' const merge = require('webpack-merge') const prodEnv = requi ...

  5. Python 对象(type/object/class) 作用域 一等函数 (慕课--Python高级,IO并发 第二章)

    在python中一共有两种作用域:全局作用域和函数作用域全局作用域:在全局都有效,全局作用域在程序执行时创建,在程序执行结束时销毁:所有函数以外的区域都是全局作用域:在全局作用域中定义的变量,都属于全 ...

  6. python网络爬虫入门范例

    python网络爬虫入门范例 Windows用户建议安装anaconda,因为有些套件难以安装. 安装使用pip install * 找出所有含有特定标签的HTML元素 找出含有特定CSS属性的元素 ...

  7. day1_作业2(三级菜单)

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- province={'江苏省':['南京市','苏州市','无锡市'],'浙江省':['杭州市','温州 ...

  8. linux下,把屏幕竖起来

    xrandr -o left 向左旋转90度 xrandr -o right 向右旋转90度 xrandr -o inverted 上下翻转 xrandr -o normal 回到正常角度

  9. The Extinction of Some Languages【一些语言的消失】

    The Extinction of Some Languages Languages have been coming and going for thousands of years, 语言的产生和 ...

  10. JQ实现下拉加载更多

    var x=0; var isloading=0; function getUsersLimited(data) { list = list.concat(data); buildList(list) ...