题意:
从1-n的数,让你选择一些数来构造,要求每个相邻的数之间的绝对值之差有k种
题解:
先放好一个1 然后往后面插数字 先满足绝对值不同的 然后全插绝对值为1的,
代码:
#include<stdio.h>
#include<string.h>
int main()
{
int n, k, a[100005], mark[100005];
while(scanf("%d %d", &n, &k) != EOF)
{
memset(a, 0, sizeof(a));
memset(mark, 0, sizeof(mark));
a[0] = 1, mark[1] = 1;
if(k == 1)
{
printf("1");
for(int i = 2; i <= n; i++)
{
printf(" %d", i);
}
printf("\n");
continue;
}
int value = n-1,num = 0, w;
k--;
while(k > 0)
{
if(a[num] - value <= 1 || mark[a[num] - value]) w = a[num] + value;
else w = a[num] - value;
k --;
value --;
a[++num] = w;
mark[w] = 1;
}
int k = num;
if(w + 1 > n || mark[w+1])
{
int date = w-1;
while(num < n-1)
{
a[++num] = date --;
}
}
else
{
int date = w+1;
while(num < n-1)
{
a[++num] = date ++;
}
}
printf("%d", a[0]);
for(int i = 1; i <= num; i++)
{
printf(" %d", a[i]);
}
printf("\n");
}
}