您的位置:首页 > 其它

字符个数统计——来自华为OJ平台测试基础篇

2014-09-28 17:03 375 查看

/*

* 字符个数统计

* 描述: 编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。

* 知识点: 字符串,函数,指针

* 题目来源: 内部整理

* 练习阶段: 初级

* 运行时间限制: 10Sec

* 内存限制: 128MByte

* 输入:

* 输入N个字符,字符在ACSII码范围内(0~127)。

* 输出:

* 输出字符的个数。

* 样例输入: abc

* 样例输出: 3

*/

#include <iostream>

#include <map>

#include <string>

using namespace std;

void Input(std::string &str,int &strlen);

void Count(std::string str,int strlen,int &number);

int main()

{

std::string str;

int strlen = 0;

int num = 0;

Input(str,strlen);

Count(str,strlen,num);

std::cout << num << endl;

system("pause");

return 0;

}

void Input(std::string &str,int &strlen)

{

getline(std::cin,str,'\n');

strlen = str.size();

return;

}

void Count(std::string str,int strlen,int &number)

{

typedef map<char,int> celements;

char c;

celements paris;

for(int i=0;i<strlen;i++)

{

c = str.at(i);

if((c>=0) && (c<=127))

{

++paris[c];

}

}

number = paris.size();

return;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: