容器

在这里插入图片描述
在这里插入图片描述

1.lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend

Sample Output
4

#include<stdio.h>
#include<iostream>
#include<sstream>
#include<set>    
using namespace std;
int main()
{
    string s1;
    while(getline(cin,s1))   //函数里面接受一个字符串(含空格)
    {
        if(s1[0]=='#')
            break;
        stringstream stream(s1);
        set<string>ss;   //定义  保证里面的元素不重复
        string s2;
        while(stream>>s2)
        {
            ss.insert(s2);    //插入元素
        }
        cout<<ss.size()<<endl;    //不同元素个数
    }
    return 0;
}

2。Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)
            break;
        map<string,int>mp;
        string s1;
        for(int i=0;i<n;i++)
        {
            s1.erase(0,s1.length());   //清空
            cin>>s1;
            mp[s1]++;   //字符串出现的次数
        }
        map<string,int>::iterator it;   //迭代器
        int maxx=-inf;
        string s2;
        for(it=mp.begin();it!=mp.end();it++)
        {
            if((*it).second>=maxx)
            {
                s2=(*it).first;   //first指string   second指int
                maxx=(*it).second;
            }
        }
        cout<<s2<<endl;
    }
    return 0;
}

map<string,int>mp;可以理解为容器中字符串的个数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值