题意:N 个学生 编了号, 然后第i个学生不能和第i+1和第i-1坐在一起,问合法的情况下最多坐多少个人,如何做
题解:
水题YY了一下,打表处理前三种 后面的情况就是在第三种情况下往前后插数字 奇数在后 偶数在前,然后没了
代码:
#include<stdio.h>
#include<string.h>
int main()
{
int n, mymap[5005];
int mark[3][4] = {{1, 1}, {1, 1}, {2, 1, 3}};
while(scanf("%d", &n) != EOF)
{
if(n <= 3)
{
printf("%d\n", mark[n-1][0]);
for(int i = 1; i <= mark[n-1][0]; i ++)
{
if(i == 1) printf("%d", mark[n-1][1]);
else printf(" %d", mark[n-1][i]);
}
printf("\n");
continue;
}
int num = 0;
memset(mymap, 0, sizeof(mymap));
for(int i = 2; i <= n; i += 2)
{
mymap[num++] = i;
}
mymap[num++] = 1, mymap[num++] = 3;
for(int i = 5; i <= n; i += 2)
{
mymap[num++] = i;
}
printf("%d\n%d", num, mymap[0]);
for(int i = 1; i < num; i++)
{
printf(" %d", mymap[i]);
}
printf("\n");
}
}
时间: 2024-10-20 08:58:56