天天看点

HDU1121——Complete the Sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1121

题目大意:给你一串有规律的数,让你写出后面C个。

别人都说是差分,但我是根据直觉来做这道题的,因为以前学过等差数列和等比数列,和这有差不多的性质,结果真的一样。就这么递推出来了。其实当你看到第三组样例时,也同样能找出答案。

    0 1 2 3 4 5 6 7 8 9 

 ------------------------------- 

 0   1 1 1 1 1 1 1 1 1 2 11 56 

 1   0 0 0 0 0 0 0 0 1 9 45 

 2   0 0 0 0 0 0 0 1 8 36 

 3   0 0 0 0 0 0 1 7 28 

 4   0 0 0 0 0 1 6 21 

 5   0 0 0 0 1 5 15 

 6   0 0 0 1 4 10 

 7   0 0 1 3 6 

 8   0 1 2 3 

 9   1 1 1  

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int T;
    int a[110][110];
    scanf("%d",&T);
    while(T--)
    {
        int i,j,s,c;
        scanf("%d%d",&s,&c);
        for(i=1;i<=s;i++)
            scanf("%d",&a[1][i]);
        for(i=2;i<=s;i++)
            for(j=s-1;j>=1;j--)
                a[i][j]=a[i-1][j+1]-a[i-1][j];
        /*for(i=1;i<=s;i++)
        {
            for(j=1;j<=s-i+1;j++)
                printf("%d ",a[i][j]);
            cout<<endl;
        }*/
        for(i=2;i<=c+1;i++)
            a[s][i]=a[s][1];
        for(i=s-1;i>=1;i--)
        {
            int t=s-i+1+c;
            for(j=s-i+2;j<=t;j++)
                a[i][j]=a[i][j-1]+a[i+1][j-1];
        }
        /*for(i=1;i<=s+c;i++)
        {
            for(j=1;j<=s+c-i+1;j++)
                printf("%d ",a[i][j]);
            cout<<endl;
        }*/
        for(i=s+1;i<=s+c-1;i++)
            printf("%d ",a[1][i]);
        printf("%d\n",a[1][s+c]);
    }
    return 0;
}