每日一题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
Output
Input
Output
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的 最后是奇数的,于是一定不挨着。
相关文章推荐
- 一个函数能否被两个线程同时调用
- vbs查看系统开关机时间
- 05顺序队列_Queue--(栈与队列)
- LAMP+LVS+KEEPALIVED(三)
- Windows下搭建PHP开发环境
- MFC中字符集(2)——Unicode 和多字节字符集 (MBCS) 支持
- 单片机之静态数码管
- pushViewController
- 04斐波那契函数_Fibonacci--(栈与队列)
- nyoj-711最舒适的路线
- AngularJS简单的理解
- XSS的一些总结(原理+检测+防御)
- Openstack 使用
- 《大道至简》读书笔记 第1篇
- 03链栈_LinkStack--(栈与队列)
- Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom
- ios app应用中在AppDelegate设置全局变量
- BAE添加部署后使用git上传代码遇到的问题
- [倚天屠龙记] vim 查找与替换(简单替换)
- QT编写的嵌入式工业控制系统