结构体排序
专题链接:https://vjudge.net/contest/336094
A - 开门人和关门人
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1234
代码:
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct people
{
char ID[20];
int h1,m1,s1;
int h2,m2,s2;
}v[10000];
bool x(people a,people b)
{
if(a.h1!=b.h1)
return a.h1<b.h1;
if(a.m1!=b.m1)
return a.m1<b.m1;
if(a.s1!=b.s1)
return a.s1<b.s1;
}
bool y(people a,people b)
{
if(a.h2!=b.h2)
return a.h2>b.h2;
if(a.m2!=b.m2)
return a.m2>b.m2;
if(a.s2!=b.s2)
return a.s2>b.s2;
}
int main()
{
int N,M;
scanf("%d",&N);
while(N--)
{
scanf("%d",&M);
for(int i=0;i<M;i++)
{
scanf("%s",v[i].ID);
scanf("%d:%d:%d",&v[i].h1,&v[i].m1,&v[i].s1);
scanf("%d:%d:%d",&v[i].h2,&v[i].m2,&v[i].s2);
}
sort(v,v+M,x);
if(M==1)
{
printf("%s",v[0].ID);
}
else
printf("%s",v[0].ID);
printf(" ");
sort(v,v+M,y);
if(M==1)
{
printf("%s",v[0].ID);
}
else
printf("%s",v[0].ID);
printf("\n");
}
return 0;
}
B - EXCEL排序
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1862
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct people
{
char name[10];
int chengji;
char xh[10];
} v[100005];
bool x(people a,people b)
{
int l;
l=strcmp(a.xh,b.xh);
if(l>0)
{
return 0;
}
return 1;
}
bool y(people a,people b)
{
int p;
p=strcmp(a.name,b.name);
{
if(p==0)
{
int l;
l=strcmp(a.xh,b.xh);
if(l>0)
{
return 0;
}
return 1;
}
if(p>0)
{
return 0;
}
return 1;
}
}
bool z(people a,people b)
{
if(a.chengji==b.chengji)
{
int l;
l=strcmp(a.xh,b.xh);
if(l>0)
{
return 0;
}
return 1;
}
return a.chengji<b.chengji;
}
int main()
{
int c,n;
int k=0;
while(~scanf("%d %d",&n,&c))
{
k++;
if(c==0&&n==0)
{
break;
}
for(int i=0; i<n; i++)
{
scanf("%s %s %d",v[i].xh,v[i].name,&v[i].chengji);
}
if(c==1)
{
sort(v,v+n,x);
printf("Case %d:\n",k);
for(int i=0; i<n; i++)
{
printf("%s %s %d\n",v[i].xh,v[i].name,v[i].chengji);
}
}
if(c==2)
{
sort(v,v+n,y);
printf("Case %d:\n",k);
for(int i=0; i<n; i++)
{
printf("%s %s %d\n",v[i].xh,v[i].name,v[i].chengji);
}
}
if(c==3)
{
sort(v,v+n,z);
printf("Case %d:\n",k);
for(int i=0; i<n; i++)
{
printf("%s %s %d\n",v[i].xh,v[i].name,v[i].chengji);
}
}
}
return 0;
}
C - 前m大的数
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1280
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[3010];
int b[4600000];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int k=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
b[k++]=a[i]+a[j];
}
}
sort(b,b+k);
for(int i=0;i<m;i++)
{
if(i!=m-1)
{
printf("%d ",b[--k]);
}
else
{
printf("%d\n",b[--k]);
}
}
}
return 0;
}
D - 统计同成绩学生人数
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1235
代码:
#include <stdio.h>
int main()
{
int N,a[1005],x;
while(~scanf("%d",&N))
{
if(N==0)
{
break;
}
int c=0;
for(int i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&x);
for(int i=0;i<N;i++)
{
if(a[i]==x)
{
c++;
}
}
printf("%d\n",c);
}
return 0;
}
E - 稳定排序
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1872
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct people1
{
char name1[55];
int chengji1,fs;
} x[305];
struct people2
{
char name2[55];
int chengji2;
} y[305];
bool cmp(people1 a,people1 b)
{
if(a.chengji1==b.chengji1)
{
return a.fs<b.fs;
}
return a.chengji1>b.chengji1;
}
int main()
{
int N,i;
while(~scanf("%d",&N))
{
int q=0,p=0;
for(i=0; i<N; i++)
{
scanf("%s %d",x[i].name1,&x[i].chengji1);
x[i].fs=i;
}
for(i=0; i<N; i++)
{
scanf("%s %d",y[i].name2,&y[i].chengji2);
}
sort(x,x+N,cmp);
for(i=0; i<N; i++)
{
int c=strcmp(x[i].name1,y[i].name2);
if(x[i].chengji1!=y[i].chengji2)
{
p=1;
}
if(c!=0)
{
q=1;
}
}
if(q==0&&p==0)
{
printf("Right\n");
}
else if(q==1&&p==0)
{
printf("Not Stable\n");
for(i=0; i<N; i++)
{
printf("%s %d\n",x[i].name1,x[i].chengji1);
}
}
else
{
printf("Error\n");
for(i=0; i<N; i++)
{
printf("%s %d\n",x[i].name1,x[i].chengji1);
}
}
}
return 0;
}
F - What Is Your Grade?
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1084
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct people
{
int p,xh,chengji,time;
}v[205]; //数组不能开太小
int cmp(people a,people b)
{
if(a.p!=b.p)
{
return a.p>b.p;
}
return a.time<b.time;
}
bool cmp2(people a,people b)
{
return a.xh<b.xh;
}
int main()
{
int n,h,m,s;
int arr[10],brr[10];
while(~scanf("%d",&n))
{
memset(arr,0,sizeof(arr));
memset(brr,0,sizeof(brr));
if(n<0)
{
break;
}
for(int i=0;i<n;i++)
{
scanf("%d",&v[i].p);
scanf("%d:%d:%d",&h,&m,&s);
v[i].time=h*3600+m*60+s;
v[i].xh=i;
arr[v[i].p]++;
}
sort(v,v+n,cmp);
for(int i=0;i<n;i++)
{
if(v[i].p==0)
{
v[i].chengji=50;
}
if(v[i].p==1)
{
brr[1]++;
if(brr[1]<=arr[1]/2)
{
v[i].chengji=65;
}
else
{
v[i].chengji=60;
}
}
if(v[i].p==2)
{
brr[2]++;
if(brr[2]<=arr[2]/2)
{
v[i].chengji=75;
}
else
{
v[i].chengji=70;
}
}
if(v[i].p==3)
{
brr[3]++;
if(brr[3]<=arr[3]/2)
{
v[i].chengji=85;
}
else
{
v[i].chengji=80;
}
}
if(v[i].p==4)
{
brr[4]++;
if(brr[4]<=arr[4]/2)
{
v[i].chengji=95;
}
else
{
v[i].chengji=90;
}
}
if(v[i].p==5)
{
v[i].chengji=100;
}
}
sort(v,v+n,cmp2);
for(int i=0;i<n;i++)
{
printf("%d\n",v[i].chengji);
}
printf("\n");
}
return 0;
}
-
另一种答案*
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int m;
char t[10];
int b;
int s;
} p[105];
int cmp_1(node x, node y)
{
if(x.m != y.m)
return x.m > y.m;
return strcmp(x.t,y.t) < 0;
}
int cmp_2(node x, node y)
{
return x.b < y.b;
}
int main()
{
int arr[10], brr[10];
int n;
while(~scanf("%d",&n),n+1)
{
memset(arr,0,sizeof(arr));
memset(brr,0,sizeof(brr));
for(int i = 0; i < n; i++)
{
scanf("%d %s",&p[i].m,p[i].t);
p[i].b = i;
arr[p[i].m]++;
}
sort(p, p+n, cmp_1);
for(int i = 0; i < n; i++)
{
if(p[i].m == 0)
{
p[i].s = 50;
}
if(p[i].m == 1)
{
brr[1]++;
if(brr[1] <= arr[1]/2)
p[i].s = 65;
else
p[i].s = 60;
}
if(p[i].m == 2)
{
brr[2]++;
if(brr[2] <= arr[2]/2)
p[i].s = 75;
else
p[i].s = 70;
}
if(p[i].m == 3)
{
brr[3]++;
if(brr[3] <= arr[3]/2)
p[i].s = 85;
else
p[i].s = 80;
}
if(p[i].m == 4)
{
brr[4]++;
if(brr[4] <= arr[4]/2)
p[i].s = 95;
else
p[i].s = 90;
}
if(p[i].m == 5) p[i].s = 100;
}
sort(p, p+n, cmp_2);
for(int i = 0; i < n; i++)
printf("%d\n",p[i].s);
printf("\n");
}
return 0;
}
G - 今年暑假不AC
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2037
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct node
{
int s,e;
}a[105];
bool cmp(node a,node b)
{
return a.e<b.e;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)
{
break;
}
for(int i=0;i<n;i++)
{
scanf("%d %d",&a[i].s,&a[i].e);
}
sort(a,a+n,cmp);
int ans=1,t=0;
for(int i=1;i<n;i++)
{
if(a[i].s>=a[t].e)
{
ans++;
t=i;
}
}
printf("%d\n",ans);
}
return 0;
}
H - Bear and Three Balls
原题链接:http://codeforces.com/problemset/problem/653/A
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int ti[1010];
int a[1010];
int main()
{
int n;
scanf("%d",&n);
int flag=0;
for(int i=0;i<n;i++)
{
scanf("%d",&ti[i]);
a[ti[i]]=1;
}
for(int i=0;i<1005;i++)
{
if(a[i]==1&&a[i+1]==1&&a[i+2]==1)
{
printf("YES\n");
flag =1;
break;
}
}
if(flag==0)
{
printf("NO\n");
}
return 0;
}
来源:CSDN
作者:Water_Coder
链接:https://blog.csdn.net/qq_45856289/article/details/103936181