首先说下结构体这种东西,就是,具有同种结构的一堆变量,可以塞到一个叫struct的东西里,声明方法如下。(ps. 别忘了加头文件<cstdlib>)再透露一下,由于本人代码能力菜的一*,所以也是不久前才掌握了一丢丢结构体排序的精髓T_T
const int Maxx = 1e2 + 7;
struct PP {
int x;
char s[Maxx];
} a[Maxx]; //结构体变量
好理解吧,调用的时候,就,拿输入举个例子吧,喏~就是这样:
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d %s", &a[i].x, a[i].s);
下面主要讲一下结构体的sort排序,因为我只会这一种。。。额。。。有点尴尬。。。
OK ~ here we go ~
首先贴波代码自己感受下:
举个栗子~比如有若干个人,他们在一场比赛中有不同得分,输入这些人的分数和名字,然后按名次输出这些人。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Maxx = 1e3 + 7;
int n;
struct PP {
int x;
char s[Maxx];
} a[Maxx];
bool cmp1(PP m, PP n) {return m.x > n.x;} //按x降序
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d %s", &a[i].x, a[i].s);
sort(a + 1, a + n + 1, cmp1);
for(int i = 1; i <= n; i++) printf("%s\n", a[i].s);
return 0;
}
这个就是按照得分x降序排的一个代码~
再来一波,再感受下~没题意,编不出来了。。。就是个,裸板子,感受下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
struct PP{
int x;
int y;
} a[5];
bool cmp1(PP m, PP n) {return m.x < n.x;} //按x升序
bool cmp2(PP m, PP n) {return m.y > n.y;} //按y降序
bool cmp3(PP m, PP n) { //先按x升序,若x相等,再按y升序
if(m.x == n.x) return m.y < n.y;
return m.x < n.x;
}
int main() {
scanf("%d %d %d %d %d %d", &a[1].x, &a[1].y, &a[2].x, &a[2].y, &a[3].x, &a[3].y);
printf("\n");
sort(a + 1, a + 4, cmp1);
for(int i = 1; i <= 3; i++) printf("%d %d\n", a[i].x, a[i].y); //按x升序排,竖向打印
printf("\n");
sort(a + 1, a + 4, cmp2);
for(int i = 1; i <= 3; i++) printf("%d %d\n", a[i].x, a[i].y); //按y降序排,竖向打印
printf("\n");
sort(a + 1, a + 4, cmp3);
for(int i = 1; i <= 3; i++) printf("%d %d\n", a[i].x, a[i].y); //先按x升序,若x相等,再按y升序
}
ok ~ 感受的咋样,刺激不,主要是sort的写法,别写挫了,如果结构体内几者中有某属性值一样,就按另一种属性排,技巧就是,倒着写,懂我意思吗...不懂的话。。。来到题感受下 ~ 看好了啊 ~
《清橙A1030 --- 球队排名》
题意:
计算机系学生会体育部组织了一次“酒井杯”足球联赛,有若干支球队参加,现在联赛已经圆满落幕,各队的积分、净胜球、进球数已经计算出来了,现在要你编写一个程序计算各支球队的排名,然后按照名次从高到低的顺序将各个球队的名字打印出来。
球队的规则如下:
先比较积分,积分高者名次在前;
如果积分相同则比较净胜球,净胜球多者名次在前;
如果积分、净胜球都相同则比较进球数,进球数多者名次在前;
对于积分、净胜球和进球数都一样的球队,根据他们的名称按照字典顺序排列。
现在假设各个球队的名称都是由大写英文字母组成,并且没有重名的球队。
输入:
第一行是一个正整数N( 2<= N <= 10)代表球队数目
接下来有N行,每一行均包括一个字符串和3个整数,
其中字符串代表球队的名称,长度在2与20之间,3个整数依次为球队的积分,净胜球和进球数,
其中积分和进球数是0到100间的整数,净胜球的绝对值小于100。
输出:
共N行,每行输出一个字符串,即球队的名称。
样例:
Sample Input
4
ACMILAN 5 1 5
SCHALKE 5 1 7
FENERBAHCE 4 -1 7
PSV 7 -1 2
Sample Output
PSV
SCHALKE
ACMILAN
FENERBAHCE
本人AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int Maxx = 1e5 + 7;
const int MaxN = 27;
const int Inf = 1e9 + 7;
const double PI = acos(-1.0);
map <string, int> mp;
int n;
struct PP {
char s[MaxN]; //队名
int a, b, c; //积分, 净胜球, 进球数
} t[15];
int cmp(PP m, PP n) { //根据题意倒着写
if(m.c == n.c && m.b == n.b && m.a == n.a) { //如果a, b, c均相等按字典序升序排
if(strcmp(m.s, n.s) < 0) return strcmp(m.s, n.s);
}
if(m.b == n.b && m.a == n.a) return m.c > n.c; //若a, b相等按c降序排
if(m.a == n.a) return m.b > n.b; //若a相等按b降序排
return m.a > n.a; //按a降序排
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%s %d %d %d", t[i].s, &t[i].a, &t[i].b, &t[i].c);
sort(t + 1, t + n + 1, cmp);
for(int i = 1; i <= n; i++) printf("%s\n", t[i].s);
return 0;
}
ok ~ 结构体快排就先说到这里啦,用的时候要考虑到sort有一个log的复杂度哦!
const int Maxx = 1e2 + 7;
struct PP {
int x;
char s[Maxx];
} a[Maxx]; //结构体变量
好理解吧,调用的时候,就,拿输入举个例子吧,喏~就是这样:
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d %s", &a[i].x, a[i].s);
下面主要讲一下结构体的sort排序,因为我只会这一种。。。额。。。有点尴尬。。。
OK ~ here we go ~
首先贴波代码自己感受下:
举个栗子~比如有若干个人,他们在一场比赛中有不同得分,输入这些人的分数和名字,然后按名次输出这些人。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Maxx = 1e3 + 7;
int n;
struct PP {
int x;
char s[Maxx];
} a[Maxx];
bool cmp1(PP m, PP n) {return m.x > n.x;} //按x降序
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d %s", &a[i].x, a[i].s);
sort(a + 1, a + n + 1, cmp1);
for(int i = 1; i <= n; i++) printf("%s\n", a[i].s);
return 0;
}
这个就是按照得分x降序排的一个代码~
再来一波,再感受下~没题意,编不出来了。。。就是个,裸板子,感受下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
struct PP{
int x;
int y;
} a[5];
bool cmp1(PP m, PP n) {return m.x < n.x;} //按x升序
bool cmp2(PP m, PP n) {return m.y > n.y;} //按y降序
bool cmp3(PP m, PP n) { //先按x升序,若x相等,再按y升序
if(m.x == n.x) return m.y < n.y;
return m.x < n.x;
}
int main() {
scanf("%d %d %d %d %d %d", &a[1].x, &a[1].y, &a[2].x, &a[2].y, &a[3].x, &a[3].y);
printf("\n");
sort(a + 1, a + 4, cmp1);
for(int i = 1; i <= 3; i++) printf("%d %d\n", a[i].x, a[i].y); //按x升序排,竖向打印
printf("\n");
sort(a + 1, a + 4, cmp2);
for(int i = 1; i <= 3; i++) printf("%d %d\n", a[i].x, a[i].y); //按y降序排,竖向打印
printf("\n");
sort(a + 1, a + 4, cmp3);
for(int i = 1; i <= 3; i++) printf("%d %d\n", a[i].x, a[i].y); //先按x升序,若x相等,再按y升序
}
ok ~ 感受的咋样,刺激不,主要是sort的写法,别写挫了,如果结构体内几者中有某属性值一样,就按另一种属性排,技巧就是,倒着写,懂我意思吗...不懂的话。。。来到题感受下 ~ 看好了啊 ~
《清橙A1030 --- 球队排名》
题意:
计算机系学生会体育部组织了一次“酒井杯”足球联赛,有若干支球队参加,现在联赛已经圆满落幕,各队的积分、净胜球、进球数已经计算出来了,现在要你编写一个程序计算各支球队的排名,然后按照名次从高到低的顺序将各个球队的名字打印出来。
球队的规则如下:
先比较积分,积分高者名次在前;
如果积分相同则比较净胜球,净胜球多者名次在前;
如果积分、净胜球都相同则比较进球数,进球数多者名次在前;
对于积分、净胜球和进球数都一样的球队,根据他们的名称按照字典顺序排列。
现在假设各个球队的名称都是由大写英文字母组成,并且没有重名的球队。
输入:
第一行是一个正整数N( 2<= N <= 10)代表球队数目
接下来有N行,每一行均包括一个字符串和3个整数,
其中字符串代表球队的名称,长度在2与20之间,3个整数依次为球队的积分,净胜球和进球数,
其中积分和进球数是0到100间的整数,净胜球的绝对值小于100。
输出:
共N行,每行输出一个字符串,即球队的名称。
样例:
Sample Input
4
ACMILAN 5 1 5
SCHALKE 5 1 7
FENERBAHCE 4 -1 7
PSV 7 -1 2
Sample Output
PSV
SCHALKE
ACMILAN
FENERBAHCE
本人AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int Maxx = 1e5 + 7;
const int MaxN = 27;
const int Inf = 1e9 + 7;
const double PI = acos(-1.0);
map <string, int> mp;
int n;
struct PP {
char s[MaxN]; //队名
int a, b, c; //积分, 净胜球, 进球数
} t[15];
int cmp(PP m, PP n) { //根据题意倒着写
if(m.c == n.c && m.b == n.b && m.a == n.a) { //如果a, b, c均相等按字典序升序排
if(strcmp(m.s, n.s) < 0) return strcmp(m.s, n.s);
}
if(m.b == n.b && m.a == n.a) return m.c > n.c; //若a, b相等按c降序排
if(m.a == n.a) return m.b > n.b; //若a相等按b降序排
return m.a > n.a; //按a降序排
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%s %d %d %d", t[i].s, &t[i].a, &t[i].b, &t[i].c);
sort(t + 1, t + n + 1, cmp);
for(int i = 1; i <= n; i++) printf("%s\n", t[i].s);
return 0;
}
ok ~ 结构体快排就先说到这里啦,用的时候要考虑到sort有一个log的复杂度哦!