Description
给出一个只由01组成的序列,要求将这个序列分成若干段,每段仅含一个1,问有多少种划分方法
Input
第一行为一整数n表示序列长度,之后n个整数表示该序列(1<=n<=100)
Output
输出划分方案数
Sample Input
5
1 0 1 0 1
Sample Output
4
Solution
简单计数,记录每个1的位置pos[i],假设有res个1,那么ans=(pos[2]-pos[1]) * (pos[3]-pos[2]) * … * (pos[res]-pos[res-1])
Code
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 111
int n,a[maxn],res,pos[maxn];
long long ans;
int main()
{
while(~scanf("%d",&n))
{
res=0;ans=1ll;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)pos[++res]=i;
}
if(res==0)ans=0;
for(int i=1;i<res;i++)
ans*=(pos[i+1]-pos[i]);
printf("%I64d\n",ans);
}
return 0;
}