Computer Transformation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7053 Accepted Submission(s): 2565
Problem Description
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.
How many pairs of consequitive zeroes will appear in the sequence after n steps?
Input
Every input line contains one natural number n (0 < n ≤1000).
Output
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
Sample Input
2
3
Sample Output
1
1
题目意思:就是 就每次转换0的;
step0: 1
step1:01
step2:10 01
step3:0110 1001
step4:1001 0110 0110 1001
step5:0110 1001 1001 0110 1001 0110 0110 1001
由这个过程可以看出:每step的00,是由step-2的1的个数,和00的个数产生的 1的个数是 2^(n-3) 所以得 a[n] =a[n-2]+2^(n-3)
但是再多产生一些列子,可以得出a[n]=a[n-1]+2*a[n-2];
再然后就注意大数问题,手动模拟相加算法;
整体来说不是很难的题。
这里写代码片
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int a[1005][1001];
void db()
{
memset(a,0,sizeof(a));
int i,j,k,len;
a[2][1]=1,a[3][1]=1,a[4][1]=3,a[5][1]=5,a[6][1]=1,a[6][2]=1;;
for(i=7;i<=1000;i++)
{
len=1;
for(k=1000;k>=1;k--)//求出a[i-1]的长度;
{
if(a[i-1][k]!=0)
{
len=k;
break;
}
}
for(k=1;k<=len;k++)
{
a[i][k]=a[i-1][k]+2*a[i-2][k];//最低位相加,不足的补零;
}
for(j=1;j<=1000;j++)
{
if(a[i][j]>=10)
{
int m;
m=a[i][j]/10;
a[i][j]%=10;
a[i][j+1]+=m;
}
}
}
}
int main()
{
db();
int n;
while(cin>>n)
{
if(n==1)
{
cout<<"0"<<endl;
continue;
}
int flag=0;
for(int j=1000;j>=1;j--)
{
if(a[n][j]!=0&&flag==0)
{
cout<<a[n][j];
flag=1;
}
else if(flag==1)
cout<<a[n][j];
}
cout<<endl;
}
return 0;
}