题解 | #求长方体表面积#

合并表记录

http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

#include<bits/stdc++.h>
//键值对且存在相同的键
using namespace std;
int main(){
    int num,index,value;
    cin>>num;
    multimap<int, int> mymmap;//允许存在相同的键
    map<int,int> mymap;//键唯一
    //解析输入并导入multimap
    while(cin>>index>>value){
        mymmap.insert(pair<int, int>(index,value));
    }
    //将mymmap里的数据按指定规则导入mymap
    for(auto m:mymmap){
        if (mymmap.count(m.first)!=1){//如果存在相同的键
            int temp = 0;
            auto pairfound = mymmap.find(m.first);//找出所有该键值的键值对
            for(size_t counter = 0; counter<mymmap.count(m.first); ++counter){
                temp += pairfound->second;//取和
                ++pairfound;//下一个
            }
            mymap.insert(pair<int, int>(m.first,temp));//导入mymap
        }
        else{//如果改键仅有一个
            mymap.insert(pair<int,int>(m.first,m.second));//直接导入mymap
        }
    }
    for(auto m:mymap){//输出
        cout<<m.first<<" "<<m.second<<endl;
    }
    return 0;
}
全部评论

相关推荐

04-06 16:59
已编辑
河南工业大学 Java
牛牛牛的牛子:最好扔了,实在没有选择的选择
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务