符号三角形_hdu_2510(深搜).java
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): 729 Accepted Submission(s): 361
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
import java.util.Scanner;
public class Main{//打表AC
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a[]={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};
while(true){
int n=input.nextInt();
if(n==0)
break;
System.out.println(n+" "+a[n]);
}
}
}
import java.util.Scanner;
public class Main{//深搜超时
static int n,sum;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while((n=input.nextInt())!=0){
sum=0;
String b=new String("");
dfs(0,b);
System.out.println(n+" "+sum);
}
}
private static void dfs(int id, String b) {
if(id==n){
// System.out.println(b);
jc(b);
return;
}
dfs(id+1,b+"+");
dfs(id+1,b+"-");
}
private static void jc(String str) {
char s[]=str.toCharArray();
long a1=0,a2=0;
for(int i=0;i<s.length;i++){
if(s[i]=='+')a1++;
else a2++;
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=n-i;j++){
if(s[j-1]==s[j]){
a1++;
s[j-1]='+';
}
else{
a2++;
s[j-1]='-';
}
}
}
if(a1==a2){
//System.out.println("&&&&"+str);
sum++;
}
}
}
*import java.util.Scanner;
public class Main{//打表
static int n,sum;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for(n=1;n<25;n++){
sum=0;
String b=new String("");
dfs(0,b);
System.out.print(sum+",");
}
}
private static void dfs(int id, String b) {
if(id==n){
// System.out.println(b);
jc(b);
return;
}
dfs(id+1,b+"+");
dfs(id+1,b+"-");
}
private static void jc(String str) {
char s[]=str.toCharArray();
long a1=0,a2=0;
for(int i=0;i<s.length;i++){
if(s[i]=='+')a1++;
else a2++;
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=n-i;j++){
if(s[j-1]==s[j]){
a1++;
s[j-1]='+';
}
else{
a2++;
s[j-1]='-';
}
}
}
if(a1==a2){
//System.out.println("&&&&"+str);
sum++;
}
}
}
符号三角形_hdu_2510(深搜).java的更多相关文章
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 符号三角形——F
F. 符号三角形 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: Java class name: 符号 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDOJ/HDU 1015 Safecracker(深搜)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
- ZOJ(ZJU) 1002 Fire Net(深搜)
Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...
- 符号三角形(hdu 2510 搜索+打表)
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Hidden String(深搜)
Hidden String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- HDU1342 Lotto 【深搜】
Lotto Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
随机推荐
- Netstat -tln 命令是Linux查看端口使用情况
Netstat -tln 命令是Linux查看端口使用情况
- PHP视频教程 字符串处理函数(三)
字符串替换函数: str_replace() 替换字符串或数组元素,区分大小,第四个参数可选用于统计替换次数. str_ireplace() 不区分大小写替换 字符串函数比较 strcmp()比较字符 ...
- [NOI2011]阿狸的打字机 --- AC自动机 + 树状数组
[NOI2011] 阿狸的打字机 题目描述: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...
- HDU 6136 Death Podracing(循环链表)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6136 [题目大意] 一堆人在操场上跑步,他们都有一定的速度和初始位置, 当两个人相遇的时候编号较小 ...
- BZOJ 2434 [Noi2011]阿狸的打字机(AC自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题目大意] 给出一个打印的过程,'a'-'z'表示输入字母,P表示打印该字符串 ...
- [CC-STREETTA]The Street
[CC-STREETTA]The Street 题目大意: 给定两个长度为\(n(n\le10^9)\)的数列\(A\)和\(B\),开始数列\(A\)中每一项值为\(-\infty\),数列\(B\ ...
- javaWeb导出POI创建的多个excel的压缩文件
文件效果图: 接口代码: //测试 http://localhost:8080/admin/test/test/poizip @RequestMapping(value = "/poizip ...
- sqlserver -- 学习笔记(二)“SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问”解决方法
将数据表导出到excel时出现下面错误: SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问,因为此组件已作为此服务器安全配置的一部分而 ...
- Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp
B. Before an Exam 题目连接: http://www.codeforces.com/contest/4/problem/B Description Tomorrow Peter has ...
- HP-unix如何生成动态库?
HP-unix系统生成动态库编译的方法? 创建PIC中间文件的编译器选项是+z,创建动态库的链接器标志是-b. 1. cc +z d1.c d2.c /* 编译以".o"为扩展名的 ...