UVA 1546

UVA 1546 - Complete the sequence!

题目链接

题意:给定多项式前s项,求出后c项,要求尽量小

思路:利用差分法,对原序列求s - 1次差分,就能够发现规律,然后对于每多一项,就逆推回去就可以

代码:

#include <stdio.h>
#include <string.h>

const int N = 205;
int t, s, c, a[N][N];

int main() {
    scanf("%d", &t);
    while (t--) {
	scanf("%d%d", &s, &c);
	memset(a, 0, sizeof(a));
	for (int i = 0; i < s; i++)
	    scanf("%d", &a[0][i]);
	for (int i = 1; i < s; i++)
	    for (int j = 0; j < s - i; j++)
		a[i][j] = a[i - 1][j + 1] - a[i - 1][j];
	for (int i = 0; i < c; i++) {
	    for (int j = s - 1; j >= 0; j--)
		a[j][s - j + i] = a[j + 1][s - j - 1 + i] + a[j][s - j - 1 + i];
	    printf("%d%c", a[0][s + i], (i == c - 1 ? '
' : ' '));
	}
    }
    return 0;
}

原文地址:https://www.cnblogs.com/mengfanrong/p/4007161.html