hdu1004 Let the Balloon Rise && hdu1263 水果(map)

本文通过两个示例程序介绍如何利用C++标准库中的map组件进行数据统计与处理。第一个程序展示了如何统计字符串出现的次数并找出出现次数最多的字符串;第二个程序则进一步演示了如何利用嵌套的map来组织更复杂的数据结构,实现对特定类型数据的统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要是想学学map

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <string.h>
using namespace std;

const int N = 20;
const int INF = 10000000;

map <string, int> M;

int main()
{
  //  freopen("in.txt", "r", stdin);
    int T, row, line;
    string st, ans;
    while(~scanf("%d", &T) && T)
    {
        M.clear();
        while(T --)
        {
            cin >> st;
            M[st] ++;
        }
        map <string, int> :: iterator it;
        int maxx = 0;
        for(it = M.begin(); it != M.end(); it ++)
        {
            if(maxx < it -> second)
            {
                maxx = it -> second;
                ans = it -> first;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

两个贴在一起吧,供学习,都不是自己想出来的= =。做这道题以前居然忘了map内部的自动排序功能。而且嵌套两层后居然可以像二维数组一样,主要就是迭代器的改变。既然可以调用2层,那3层呢,4层呢,5。。。mad.gifuploading.4e448015.gif转存失败重新上传取消生气

 

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cctype>
#include <string.h>
using namespace std;

const int N = 3010;
const int INF = 10000000;

map <string, map<string, int> > M;

int main()
{
  //  freopen("in.txt", "r", stdin);
    int n, m, num, flag = 0;
    scanf("%d", &n);
    string name, from;
    while(n --)
    {
        if(flag) printf("\n");
        flag = 1;
        M.clear();
        scanf("%d", &m);
        while(m --)
        {
            cin >> name >> from >> num;
            M[from][name] += num;
        }
        map <string, map<string, int> > :: iterator it;
        map <string, int> :: iterator its;
        for(it = M.begin(); it != M.end(); it ++)
        {
            cout << it->first << endl;
            for(its = it->second.begin(); its != it->second.end(); its ++)
            {
                cout << "   |----" << its->first << "(" << its->second << ")" << endl;
            }
        }
    }
    return 0;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值