zoukankan      html  css  js  c++  java
  • 符号三角形(hdu 2510 搜索+打表)

    符号三角形

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


    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
     
    打表代码:
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 int a[25];
     6 char res[25][25];
     7 int    sum;
     8 bool count(int n)
     9 {
    10     int p1=0,p2=0;
    11     int i,j;
    12     for(i=1;i<=n;i++)
    13     {
    14         if(res[0][i]=='+')    p1++;
    15         else    p2++;
    16     }
    17     for(int i=1;i<n;i++)
    18         for(j=1;j<=n-i;j++)
    19         {
    20             res[i][j]=(res[i-1][j]==res[i-1][j+1]?'+':'-');
    21             if(res[i][j]=='+')    p1++;
    22             else    p2++;
    23         }
    24     if(p1==p2)
    25         return 1;
    26     return 0;
    27 }
    28 void dfs(int n,int s)
    29 {
    30     int i,j;
    31     if(s>n)
    32     {
    33         if(count(n))
    34             sum++;
    35         return;
    36     }
    37     res[0][s]='+';
    38     dfs(n,s+1);
    39     res[0][s]='-';
    40     dfs(n,s+1);
    41     return;
    42 }
    43 int main()
    44 {
    45     int n;
    46     int i,j;
    47     a[1]=a[2]=0;
    48     for(i=1;i<=20;i++)
    49     {
    50         sum=0;
    51         dfs(i,1);
    52         a[i]=sum;
    53         cout<<a[i]<<endl;
    54     }
    55 }
  • 相关阅读:
    Unity动态批处理和静态批处理学习
    Mac下Unity使用Jenkins自动化打包
    Mac 环境环境下安装Git与使用(码云)
    MySQL: 6、MySQL语句
    MySQL: 5、MySQL索引、视图、存储过程
    MySQL: 4、多表、外键、数据库设计
    MySQL: 3、SQL语言 ②约束、事务
    MySQL: 2、SQL语言 ①概念、分类
    MySQL: 1、MySQL基础
    Mac 系统下如何显示和隐藏文件
  • 原文地址:https://www.cnblogs.com/a1225234/p/5024093.html
Copyright © 2011-2022 走看看