输入样例:
5
4 6 7 6
输出样例:
3 1 5 2 4
C++代码(枚举,递推)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1300;
int n;
int a[N],b[N];
bool st[N];
int check(int a1)
{
fill(st, st + n + 1 , 0); // 初始化标记数组
st[a1] = true; // 用过的不能用
a[1] = a1;
for(int i = 2; i <= n; i++)
{
a[i] = b[i - 1] - a[i - 1]; // 递推
if(a[i] < 1 || a[i] > n ) return false;// 超过范围,则不符合条件
if(st[a[i]]) return false; // 用过的数,则不符合条件
st[a[i]] = true; // 标记用过的数
}
return true;
}
int main()
{
cin >> n;
for(int i = 1; i <= n - 1; i++) cin >> b[i];
for(int i = 1; i <= b[1]; i++) // 枚举a[1] 是否符合题意
{
if(check(i))
{
for(int i = 1; i <= n; i++) cout << a[i] << " ";
break;
}
}
return 0;
}