首页 > 其他 > 详细

P1044 栈

时间:2019-08-04 09:47:42      阅读:105      评论:0      收藏:0      [点我收藏+]

P1044 栈

题解

记忆化搜索了解一下???

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>

using namespace std;

inline int read()
{
    int ans=0;
    char last= ,ch=getchar();
    while(ch<0||ch>9) last=ch,ch=getchar();
    while(ch>=0&&ch<=9) ans=ans*10+ch-0,ch=getchar();
    if(last==-) ans=-ans;
    return ans;
}

int n;
int f[20][20];

int dfs(int x,int y)  //待操作序列里面还有x个数字,已经有y个数字入栈 
{
    if(f[x][y]!=0) return f[x][y]; //记忆化过了 
    if(x==0) return 1; //没有待操作数字了,只能全部弹出栈里的数字 
    f[x][y]+=dfs(x-1,y+1);  //考虑新数字入栈 
    if(y>0) f[x][y]+=dfs(x,y-1);  //考虑栈顶出栈 
    return f[x][y];
}

int main()
{
    n=read();
    printf("%d",dfs(n,0));
    return 0;
}


 

 

 

 

DP也了解一下??

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>

using namespace std;

inline int read()
{
    int ans=0;
    char last= ,ch=getchar();
    while(ch<0||ch>9) last=ch,ch=getchar();
    while(ch>=0&&ch<=9) ans=ans*10+ch-0,ch=getchar();
    if(last==-) ans=-ans;
    return ans;
}

int n;
int f[20][20];

int main()
{
    n=read();
    for(int i=1;i<=n;i++)
      f[0][i]=1;
    
    for(int i=1;i<=n;i++)
      for(int j=i;j<=n;j++) 
      {
          if(i==j) f[i][j]=f[i-1][j];
          else f[i][j]=f[i][j-1]+f[i-1][j];
      }
    
    printf("%d",f[n][n]);
    return 0;
}


 

 

P1044 栈

原文:https://www.cnblogs.com/xiaoyezi-wink/p/11297059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!