您的位置:首页 > 其它

每日一题E

2015-06-06 16:14 169 查看
Description

An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i
and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.

Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.

Input

A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.

Output

In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.

In the second line print k distinct integers
a1, a2, ..., ak (1 ≤ ai ≤ n),
where ai is the number of the student on the
i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true:
|ai - ai + 1| ≠ 1 for all
i from 1 to
k - 1.

If there are several possible answers, output any of them.

Sample Input

Input
6


Output
6
1 5 3 6 2 4


Input
3


Output
2
1 3


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

int main()
{
int a[5050],b[5050];
int n,i,j=0,k=0;
scanf("%d",&n);
if(n==1)
printf("1\n1");
if(n==2)
printf("1\n1");
if(n==3)
printf("2\n1 3");
if(n==4)
printf("4\n3 1 4 2");
if(n>4)
{

for(i=5; i<=n; i++)
{

if(i%2==0)
{
b[j]=i;
j++;
}
if(i%2!=0)
{
a[k]=i;
k++;
}

}

printf("%d\n",4+j+k);
for(i=0;i<j;i++)
printf("%d ",b[i]);
printf("3 1 4 2 ");
for(i=0;i<k;i++)
printf("%d ",a[i]);
}
return 0;
}
这个题找规律找到4,以后有多少人就排多少人,那么提前把前四种写出来。从5开始往后 为了让数字不挨着可以把数据分为奇数和偶数,输出让4隔开,可以把4设置好了第一个为奇数,最后一个为偶数。那么前面输出数组是偶数的,中间是4的 最后是奇数的,于是一定不挨着。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: