符号三角形_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 ...
随机推荐
- python邮件
解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...
- java8新特性——四大内置核心函数式接口
在前面几篇简单介绍了一些Lambda表达式得好处与语法,我们知道使用Lambda表达式是需要使用函数式接口得,那么,岂不是在我们开发过程中需要定义许多函数式接口,其实不然,java8其实已经为我们定义 ...
- FileZilla提权的过程
话不多说,我们直接操作! 首先我们通过 webshell 找到 FileZilla 的文件目录,找到 FileZilla Server Interface.xml 文件,打开.我们可以看到它的连接地址 ...
- Spring Boot中Request method 'PUT' not supported
在项目中使用restful风格put提交时报错,是由于form表单中的th:href引起的(支持post提交),改为th:action即可
- 在活动中使用菜单(Menu)
任务名称:在活动使用菜单 任务现象:打开程序后,点击菜单按钮会出现2个选项,点击选项时会跳出相对应的提示框 步骤 1.创建一个项目,详细参考:http://8c925c9a.wiz03.com/sha ...
- python开发_counter()
在python的API中,提到了Counter,它具有统计的功能 下面是我做的demo: 1.统计自定义字符串中每个字符出现的次数 2.读取一个文件,把文件中的内容转化为字符串,统计该字符串中每个字符 ...
- BFC与hasLayout
BFC与hasLayout都是CSS布局上的概念. 几个月前在微博上才了解什么是BFC,算是对布局有点初步的了解. hasLayout则是IE6.7产生许多bug的根源. 一.BFC Floats, ...
- <摘录>CentOS怎么查看某个命令的源代码
安装yumdownloader工具: # yum install yum-utils 设置源: [base-src] name=CentOS-5.4 - Base src - baseurl=http ...
- Delphi CompilerVersion Constant / Compiler Conditional Defines
http://delphi.wikia.com/wiki/CompilerVersion_Constant The CompilerVersion constant identifies the in ...
- PHP 函数之 call_user_func & call_user_func_array
call_user_func_array (callable $callback, array $param_arr) 參数1: 调用一个回调函数, 參数2: 数组參数是回调函数的參数. call_u ...