HDU - 3293B - sort与HDU - 1263 水果 pair的使用

事例1:
as is known to all, long long ago sailormoon once was an association of fighters. Till now, sailormoon is also an association of girls. Owe to some unknown reasons, girls are necessary to fight for peace. 
Their boss, lcy, wants to strengthen their ability, so he give them his precious collections---weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so). 
In order to make it clear, girls want to sort like this: 
firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together; 
secondly, sort according ranks, wonderful is the best, good is next, the third is so-so; 
thirdly, if two or more have same origin and rank, sort them according to the lexicographic order. 


Input
Input contains multiply cases. Each case contains several lines. First line is an integer N(0<N<=500), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness. 
Each string will not exceed 20 characters. 
Sure that same origin will not exist the same weapon.
Output
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
Sample Input
5
knife qizhou so-so
gun qizhou wonderful
knife zhengzhou good
stick zhengzhou good
rope shengzhou so-so
Sample Output
Case 1
qizhou:
          gun wonderful
          knife so-so
shengzhou:
          rope so-so
zhengzhou:
          knife good

stick good

思路:就是按照字典序排列,此题主要是要求掌握pair二元组的用法

1.结构体写法:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdio>
typedef long long ll;
#define N 1005
using namespace std;
struct point
{
    string weapon;
    string origin;
    int harm;
} a[700];
int cmp(point x,point y)
{
    if(x.origin!=y.origin)
        return x.origin<y.origin;
    if(x.harm!=y.harm)
    {
        if(x.harm==1)
            return 1;
        if(y.harm==1)
            return 0;
        if(y.harm==3)
            return 1;
        if(x.harm==3)
            return 0;
    }
    return x.weapon<y.weapon;
}
int main()
{
    int k,icase=1;
    while(scanf("%d",&k)==1)
    {
        char iharm[50];
        for(int i=0; i<k; i++)
        {
            cin>>a[i].weapon>>a[i].origin>>iharm;
            if(iharm[0]=='w')   //数字越小优先级越高
                a[i].harm=1;
            else if(iharm[0]=='g')
                a[i].harm=2;
            else if(iharm[0]=='s')
                a[i].harm=3;
        }
        sort(a,a+k,cmp);
        for(int i=0; i<k;)
        {
            string str=a[i].origin;
            cout<<str<<":"<<endl;
            while(i<k&&str==a[i].origin)
            {
                cout<<"          "<<a[i].weapon<<" ";
                if (a[i].harm==3)
                    cout<<"so-so";
                else if (a[i].harm==2)
                    cout<<"good";
                else
                    cout<<"wonderful";
                cout<<endl;
                ++i;
            }
        }
    }

}


2.pair写法

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef pair<int ,string>P;//二元组P
typedef pair<string,P>Q;   //二元组Q

Q str[600];  //二元组数组str
string le[]={"wonderful","good","so-so"};//定义等级类型
int main()
{
    int n,icase=0,ile;
    string name,origin,level;
    while(cin>>n)
    {
        icase++;
        for(int i=0;i<n;i++)
        {
            cin>>name>>origin>>level;
            for( ile=0;ile<3;ile++)
            {
                if(level==le[ile])
                    break;
            }
            str[i]=make_pair(origin,P(ile,name)); //将元素压入二元组
        }
        sort(str,str+n);//按照Q.first 比较,如果相同,比较Q.second.first 若相同,比较Q.second.second
        cout << "Case " << icase << endl;
        cout << str[0].first << ':' << endl;
        cout << "          " << str[0].second.second <<" " << le[str[0].second.first] << endl;
           for(int i=1; i<n; i++)
        {
            if(str[i].first!=str[i-1].first)
                cout << str[i].first << ':' << endl;
            cout << "          " << str[i].second.second <<" " << le[str[i].second.first] << endl;
        }

    }
    return 0;
}
//使用二元组比较方便,如果用结构体得自己编写sort的比较,略微麻烦一点

事例2:
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了. 
Input
第一行正整数N(0<N<=10)表示有N组测试数据. 
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成. 
Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序. 
两组测试数据之间有一个空行.最后一组测试数据之后没有空行. 
Sample Input
1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1
Sample Output
guangdong
   |----pineapple(5)
   |----sugarcane(1)
shandong
   |----apple(3)
照着题目意思写即可
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;


typedef pair<string,string>P;
typedef pair<P,int >Q;
Q str[300];
int main()
{
    int icase,n;
    cin>>icase;
    while(icase--)
    {
        cin>>n;
        string fruit,origin;
        for(int i=0;i<n;i++)
            cin>>str[i].first.second>>str[i].first.first>>str[i].second;//水果,地点,数量
        sort(str,str+n);


       cout << str[0].first.first << endl;
        for(int i=1;i<n;i++)
        {
            if(str[i].first.first!=str[i-1].first.first)
            {
                cout <<"   |----"<< str[i-1].first.second << '(' << str[i-1].second << ')' << endl;
                cout << str[i].first.first << endl;
            }
            else if(str[i].first.second!=str[i-1].first.second)
                cout <<"   |----"<< str[i-1].first.second << '(' << str[i-1].second << ')' << endl;
            else
                str[i].second+=str[i-1].second;


        }
        cout <<"   |----"<< str[n-1].first.second << '(' << str[n-1].second << ')'<<endl;
        if(icase>0)
            cout<<endl;


    }
    return 0;
}



pair总结:pair表示的是一个二元组(first,second),仅含有这2个元素,但一般会进行嵌套使用(如上题),并且提供了按照字典序排序的进行大小比较的函数
规则是先比first,如果相同比second,嵌套的话也是同理,但要注意题目数据的处理,谁放在first,谁放在second,需要观察。除了定义pair之外,还可以用make_pair生成pair对象


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值