zzuli 1812: sort
2015-12-08 17:43
363 查看
http://acm.zzuli.edu.cn/problem.php?id=1812
对于 k 个用空格分隔开的整数,依次为 n1, n2 … nk。请将所有下标不能被 3 但可以被 2 整除的数在这些数字原有的位置上进行升序排列,此外,将余下下标能被 3 整除的数在这些数字原有的位置上进行降序排列。
spy想了半天不知道怎么排序,你可以帮助他么?
输入数据占一行,所有不能用!EOF读入,用字符读入,遇到回车时结束
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define N 110000
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-5
#define met(a, b) memset (a, b, sizeof (a))
int main ()
{
int b1
, b2
, a
, x;
while (scanf ("%d", &x) != EOF)
{
met (b1, 0);
met (b2, 0);
met (a, 0);
a[1] = x;
int k = 1;
char ch;
while (scanf ("%c", &ch), ch!='\n')
{
if (ch == ' ') k++;
if (ch >= '0' && ch <= '9')
a[k] = a[k]*10 + ch-'0';
}
int j1 = 0, j2 = 0;
for (int i=1; i<=k; i++)
{
if (i%2==0 && i%3)
b1[j1++] = a[i];
else if (i%3 == 0)
b2[j2++] = a[i];
}
sort (b1, b1+j1);
sort (b2, b2+j2);
j1 = 0, j2--;
for (int i=1; i<=k; i++)
{
if (i%2==0 && i%3)
a[i] = b1[j1++];
else if (i%3==0)
a[i] = b2[j2--];
}
for (int i=1; i<k; i++)
printf ("%d ", a[i]);
printf ("%d\n", a[k]);
}
return 0;
}
1812: sort
Description
想必大家对排序已经很熟悉了,但是spy好像对排序不太熟悉,有一天,他看到这样一个关于排序的题目:对于 k 个用空格分隔开的整数,依次为 n1, n2 … nk。请将所有下标不能被 3 但可以被 2 整除的数在这些数字原有的位置上进行升序排列,此外,将余下下标能被 3 整除的数在这些数字原有的位置上进行降序排列。
spy想了半天不知道怎么排序,你可以帮助他么?
Input
多组数据,每组数据一行,为k个小于1000的正整数,依次为 n1, n2 … nk。(1 <= k <= 100000)Output
对于每组数据,输出排序后的结果。Sample Input
1 3 4 2 10 6 8
Sample Output
1 2 6 3 10 4 8
输入数据占一行,所有不能用!EOF读入,用字符读入,遇到回车时结束
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define N 110000
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-5
#define met(a, b) memset (a, b, sizeof (a))
int main ()
{
int b1
, b2
, a
, x;
while (scanf ("%d", &x) != EOF)
{
met (b1, 0);
met (b2, 0);
met (a, 0);
a[1] = x;
int k = 1;
char ch;
while (scanf ("%c", &ch), ch!='\n')
{
if (ch == ' ') k++;
if (ch >= '0' && ch <= '9')
a[k] = a[k]*10 + ch-'0';
}
int j1 = 0, j2 = 0;
for (int i=1; i<=k; i++)
{
if (i%2==0 && i%3)
b1[j1++] = a[i];
else if (i%3 == 0)
b2[j2++] = a[i];
}
sort (b1, b1+j1);
sort (b2, b2+j2);
j1 = 0, j2--;
for (int i=1; i<=k; i++)
{
if (i%2==0 && i%3)
a[i] = b1[j1++];
else if (i%3==0)
a[i] = b2[j2--];
}
for (int i=1; i<k; i++)
printf ("%d ", a[i]);
printf ("%d\n", a[k]);
}
return 0;
}
相关文章推荐
- rose破解安装
- js浏览器和浏览器插件检测的方法总结
- HDU 1074—压缩DP
- ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法
- 基于catalog 创建RMAN存储脚本
- 带文件上传的form表单提交, jquery的ajaxfileupload+插件dropzone图文详解
- Shell脚步之监控iostat数据
- 【四级备考】
- JSP动态网站环境搭建应用中的详细步骤(Tomcat和Apache/IIS的整合)
- 基于catalog 的RMAN 备份与恢复
- 用户空间与内核空间,进程上下文与中断上下文[总结]
- ActionBarSherlock导入Android Studio gradl额failed
- CentOS 在本地部署repo
- Java里List取并集方法retainAll不能用来判断是否有重复数据!
- BZOJ1087 [SCOI2005] 互不侵犯King
- mac os x使用Git简易入门教程
- 递归 回文数
- 杂谈PID控制算法——第一篇:三个量
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- Label的用法