sort函数再理解
-
实现的功能
实现一个数组的从小到大排序。
-
包含头文件
#include
-
之前常用的的方式为
sort(a,a+b); //a为数组,b为要进行排序的个数
-
若进行必要的从大到小时可以建立cmp函数
bool cmp(int a,int b){ return a>b; }
-
情况分析
优势:可以快速的实现排序。
但无法实现struct()数组的排序
-
劣势优化改正;
-
情况1
实现结构体struct()中单个元素的排序
模板:
#include<bits/stdc++.h> using namespace std; struct yangli{ int a; int b; }num[1000]; bool cmp(yangli a1,yangli a2){ return a1.a>a2.a; } int main(){ for(int i=0;i<100;i++){ scanf("%d",&num[i].a) } sort(num,num+100,cmp)//其中cmp规定了排序方法,只对其中的a进行排序 }
present Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there. If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift. Now Petya wants to know for each friend i the number of a friend who has given him a gift. Input The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi — the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves. Output Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i. Example 1) Input 4 2 3 4 1 Output 4 1 2 3 2) Input 2 1 2 Output 1 2 3) Input 3 1 3 2 Output 1 3 2
解决代码
#include<cstdio> #include<algorithm> using namespace std; struct Class{ int a,b; }num[105]; bool cmp(Class a1,Class a2){ return a1.b<a2.b; } int main(){ int a; while(~scanf("%d",&a)){ for(int i=1;i<=a;i++){ scanf("%d",&num[i].b); } for(int i=1;i<=a;i++){ num[i].a=i; } sort(num+1,num+a+1,cmp); for(int i=1;i<=a;i++){//跟上面给的模型基本一致 if(i!=1){ printf(" "); } printf("%d",num[i].a); } } }
-
对于同一个struct()中不同的元素的排列
比方说,对于在struct()中a元素满足某个条件对a排序,若不满足时吗、,则对b进行排序。
模板:
#include<bits/stdc++.h> using namespace std; struct yangli{ int a,b; }num[1005]; bool cmp(yamgli a1,yangli a2){ if(a1.a!=a2.a){ return a1.a>a2.a; }else{ return a1.b>a2.b;//其中我设a不为零时对a进行排序,否则对b。 }//题目多变,根据实际情况选择。 } int main(){ for(int i=0;i<100;i++){ scanf("%d %d",&num[i].a,&num[i].b) } sort (num,num+100,cmp); // 剩余部位由题而解 return 0 }
例题
Combination Ilya plays a card game by the following rules. A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded. More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards. Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards? input The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has. Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly. output Print the single number — the maximum number of points you can score in one round by the described rules. example 1) Input 2 1 0 2 0 Output 2 2) Input 3 1 0 2 0 0 2 Output 3
实现代码
#include<bits/stdc++.h> using namespace std; struct play{ int a,b; }card[1005]; bool cmp(play a1,play a2){ if(a1.b!=a2.b){ return a1.b>a2.b; } else { return a1.a>a2.a; } } int main() { int a; scanf("%d",&a); for(int i=0;i<a;i++){ scanf("%d %d",&card[i].a,&card[i].b); } sort (card,card+a,cmp); int c=0,d=1,e=0; while(d&&c<a){ d+=card[c].b; e+=card[c].a; c++; d--; } printf("%d",e); return 0; }
-
-
可能有的同学上交失败,则尝试着把头文件更换,毕竟不是所有的oj支持万能头文件#include<bits/stdc++.h>
绝大部分是#include
#include
如果有不对的欢迎来指正