C语言之不同的单词

欢迎进入我的C语言世界

题目

Problem Description

给出一个英文单词的列表,计算有多少不同的单词在列表中。

Input

本题有多组输入数据,你必须处理到EOF为止

每组数据的第一行有一个整数n, 1<=n<=1000.下面的n行每行一个单词,每个单词的长度不超过20。单词大小写忽略。

Output

每组数据输出一个整数,表示不同的单词数

Sample Input

5
FZU
FzU
LOY
BNh
FZU

Sample Output

3

答案

下面展示 实现代码

#include <iostream>
#include <stdio.h>
#include <map>
#include <string>
using namespace std;
int main()
{
    
    
	std:map<string, int>my_map;//定义了一个用string作为索引,并拥有相关联的指向int的指针.
	int n;
	string s;
	while(scanf("%d", &n) != EOF)
	{
    
    
		my_map.clear();//清空 
		for(int i = 0; i < n; i++)
		{
    
    
			cin >> s;
			for(int i=0;i<s.length();i++) 
			{
    
    
				s[i] = tolower(s[i]);
			}
			my_map[s]++;//string型的s作为索引被存入map中 
		}
		cout << my_map.size() << endl;
	}
	return 0;
 } 

本题感悟

本块内容可能来自课本或其他网站,若涉及侵权问题,请联系我进行删除,谢谢大家啦~

思路:

  1. 第一次用map,STL真的好强啊
  2. string类型的s可以用s[i]访问里面的字母
  3. s[i] = tolower(s[i]) 变小啦
    s[i] = toupper(s[i]) 变大啦

以上。

猜你喜欢

转载自blog.csdn.net/hongguoya/article/details/106373228