九度1185
题目描述:
输入一系列整数,将其中最大的数挑出,并将剩下的数进行排序。
- 1
- 2
输入:
输入第一行包括1个整数N,1<=N<=1000,代表输入数据的个数。
接下来的一行有N个整数。
- 1
- 2
- 3
输出:
可能有多组测试数据,对于每组数据,
第一行输出一个整数,代表N个整数中的最大值,并将此值从数组中去除,将剩下的数进行排序。
第二行将排序的结果输出。
- 1
- 2
- 3
- 4
样例输入:
4
1 3 4 2
- 1
- 2
- 3
样例输出:
4
1 2 3
- 1
- 2
- 3
提示:
如果数组中只有一个数,当第一行将其输出后,第二行请输出"-1"。
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(int argc, char* argv[])
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,max=-9999,maxnum=0;
int a[1000];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
if(n==1)
printf("%d\n-1",a[0]);
else{
for(i=0;i<n-1;i++)
{
if(i==0)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
}
printf("\n");
}
return 0;
}
九度1023
-
题目描述:
-
Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
-
输入:
-
测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。
-
输出:
-
对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
-
样例输入:
-
3 1 000007 James 85 000010 Amy 90 000001 Zoe 60 4 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98 4 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90 0 0
-
样例输出:
-
Case 1: 000001 Zoe 60 000007 James 85 000010 Amy 90 Case 2: 000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60 Case 3: 000001 Zoe 60 000007 James 85 000002 James 90 000010 Amy 90
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
struct S{
char sno[7];
char sname[9];
int grade;
}stu[1000];
bool cmp_sno(S s1,S s2){
return strcmp(s1.sno,s2.sno)<0;
}
bool cmp_sname(S s1,S s2){
if(strcmp(s1.sname,s2.sname)!=0) return strcmp(s1.sname,s2.sname)<0;
else return strcmp(s1.sno,s2.sno)<0;
}
bool cmp_grade(S s1,S s2){
if(s1.grade!=s2.grade) return s1.grade<s2.grade;
else return strcmp(s1.sno,s2.sno)<0;
}
int main(int argc, char* argv[])
{
int n,c,t=0;
while(scanf("%d %d",&n,&c)&&n!=0)
{
t++;
int i;
for(i=0;i<n;i++)
{
scanf("%s %s %d",&stu[i].sno,&stu[i].sname,&stu[i].grade);
}
if(c==1) sort(stu,stu+n,cmp_sno);
else if(c==2) sort(stu,stu+n,cmp_sname);
else if(c==3) sort(stu,stu+n,cmp_grade);
printf("Case %d:\n",t);
for(i=0;i<n;i++)
{
printf("%s %s %d\n",stu[i].sno,stu[i].sname,stu[i].grade);
}
}
return 0;
}
九度1054
-
题目描述:
-
输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。
-
输入:
-
测试数据有多组,输入字符串。
-
输出:
-
对于每组输入,输出处理后的结果。
-
样例输入:
-
bacd
-
样例输出:
-
abcd
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main(int argc, char* argv[])
{
char str[200];
while(gets(str))
{
sort(str,str+strlen(str));
printf("%s\n",str);
}
return 0;
}
poj2299 求逆序对(利用归并排序)
Description
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
注意两点,由于题目中所给的数据量达到了500000,所以不能用冒泡来求交换(n*n一定超时),可以用归并排序来求
另外所求结果最坏大小为500000*500000,所以需要用long long int来定义
#include<stdio.h>
#include<iostream>
using namespace std;
int ar[5000001];
int br[5000001];
long long int ans;
void merge(int a, int mid, int b) {
int i = a, j = mid + 1;
int k = a;
while (i <= mid&&j <= b) {
if (ar[i] > ar[j]) {
ans += j - k; //求逆序对
br[k++] = ar[j++];
}
else {
br[k++] = ar[i++];
}
}
//下面两个while循环都是用来处理剩余的数据
while (i <= mid) {
br[k++] = ar[i++];
}
while (j <= b) {
br[k++] = ar[j++];
}
//重新赋值更新ar数组
for (i = a; i <= b; i++) {
ar[i] = br[i];
}
}
void mergesort(int a, int b) {
if (a < b) {
int mid = (a + b) / 2;
mergesort(a, mid);
mergesort(mid + 1, b);
merge(a, mid, b);
}
}
int main() {
int n;
while (scanf("%d", &n) != EOF&&n!=0) {
ans = 0;
int i, j;
for (i = 0; i < n; i++) {
scanf("%d", &ar[i]);
}
mergesort(0, n - 1);
printf("%lld\n", ans);
}
}