hdu 1041 Computer Transformation

本文探讨了计算机序列在特定转换规则下连续零对的数量变化规律,通过实例展示了序列演化的过程,并深入分析了序列中连续零对的产生机制及数量随步骤增加的变化趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值