1013.绝对值排序
单点时限: 2.0 sec
内存限制: 256 MB
给你 n(n ≤ 100)个数,要求你把这 n 个数按照绝对值从小到大排序。
多 Case,处理直到文件结束。
输入格式
输入一个数 n,接下来一行是几个整数(-10000 <n< 10000)。
输出格式
排序后按照绝对值从小到大输出几个数,输出一行,每两个数字之间有个空格,最后一个数字后面没空格
样例
input
4 -3 -4 1 2 1 10
output
1 2 -3 -4 10
#include<stdio.h>
#include<math.h>
int main()
{
int n, i, j;
int temp;
int b, c;
int a[100];
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
b = abs(a[j]);
c = abs(a[j + 1]);
if (b> c)
{
temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
printf("%d", a[0]);
for (i = 1; i < n; i++)
{
printf(" %d", a[i]);
}
printf("\n");
}
return 0;
}
1014. Sort it...
单点时限: 2.0 sec
内存限制: 256 MB
There is a database,partychen want you to sort the database's data in the order from the least up to the greatestelement,then do the guery; "hich element is i-th by its value?"- with i being a natural number in a range from 1 to N
It should be able to process quickly queries like this
输入格式
The standard input of the problem consists oftwo parts, At first, a database is writen, and then there's a sequence ogueries. The format of database is very simple: in the first line there's a number N(1 < N< 100000), in the next Nines there are numbers of the database one in each line in an arbitrary order, A sequence of queries is written simplyas well: in the first line ofthe sequence a number ofqueries K(1 <K< 100) is written, and in the next K lines thereare queries one in each line. The guery "Which element is i-th by its value?" is coded by the number i.
输出格式
The output should consist of K lines, in each line there should be an answer to the corresponding gquery. The answer tothe query "i" is an element from the database, which is ith by its value (in the order from the least up to the greatestelement).
样例
input
5 7 121 123 7 121 3 3 2 5
output
121 7 123
#include <stdio.h>
#include<stdlib.h>
int cmp1(const void* a, const void* b)
{
return*(int*)a - *(int*)b;
}
int main()
{
int N;
scanf("%d", &N);
int a[100000];
int tmp;
for (int i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
qsort(a, N, sizeof(a[0]), cmp1);
int K;
scanf("%d", &K);
int b[100000];
for (int i = 0; i < K; i++)
{
scanf("%d", &b[i]);
}
for (int i = 0; i < K; i++)
{
tmp = b[i];
printf("%d\n", a[tmp-1]);
}
return 0;
}
1015.排序去重
单点时限: 2.0 sec
内存限制: 256 MB
有几个1到 1000 之间的整数(1 <=n<= 100),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再按照指定的排序方式把这些数排序。
输入格式
第1行为字母A或D,A表示按照升序排序,D 表示按照降序排序。
第 2行开始有若干个用一个空格或换行符分隔的正整数。
输出格式
相互之间用一个空格分隔的经去重和排序后的正整数。最后一个数后没有空格.
样例
input
A 20 40 32 67 40 20 89 300 400 15
output
15 20 32 40 67 89 300 400
#include <stdio.h>
#include<stdlib.h>
int cmp1(const void*a,const void*b)
{
return*(int*)b - *(int*)a;//这是降序
//降序需要调整*p1和*p2的减法关系
}
int cmp2(const void* a, const void* b)
{
return*(int*)a - *(int*)b;//直接return*p1-*p2的方式是直接返回结果数字,默认是升序
}
int main()
{
char order;
scanf("%c", &order);
int a[100];
int flag[1001] = { 0 };
int i=0,tmp;
while (scanf("%d", &tmp) != EOF)
{
if (flag[tmp] == 0)
{
a[i++] = tmp;
flag[tmp] = 1;
}
}
if (order == 'A')
{
//qsort(需要被排序的数组,数组元素个数,每个元素大小,排序函数名
qsort(a,i,sizeof(a[0]),cmp2);//对应的库函数是<stdlib.h>
}
if (order == 'D')
{
qsort(a, i, sizeof(a[0]), cmp1);
}
for (int j = 0; j < i - 1; j++)
printf("%d ", a[j]);
printf("%d\n", a[i-1]);
return 0;
}