统计字符串中有‘0’——‘9’的字符个数
2016-07-15 15:33
281 查看
#include<iostream> using namespace std; int Cllcdigital(char *str) { if(NULL == str){ return 0; } int i = 0; int size = 0; int count[256] ={0}; char *sd = NULL; sd = str; while(*sd != '\0'){ count[*sd] = count[*sd] +1; sd++; } for(i = '0';i<= '9';i++) { size += count[i]; } return size; } int main() { int size = 0; char str[] = "he1234llo,0909"; size = Cllcdigital(str); cout << size <<endl; return 0; } 运行结果: 8 Press any key to continue
体会:
有点类似哈希算法的思想,想象有256个桶 ,
来一个元素,就将它放入桶中,每个桶中加一
就可求出
相关文章推荐
- 为什么time()函数要返回1970年1月1日00点00分00秒开始到现在的秒数
- studio2.0+ndk-r11 中jni编译问题
- iwconfig的用法(转)
- 在studio build.gradle配置MaterialDesignLibrary
- 16暑期补HackRank计划
- RabbitMQ学习(四)之路由(java)
- 机器学习常见算法分类汇总
- Maven、gradle、Ant、Eclipse IDE 之间的关系
- 从Immutable来谈谈对于线程安全的理解误区
- 神奇的风
- linux下的GDB调试
- linux常用方法
- js弹出框 -搜索
- WebStorm11 注册
- Android 中 HttpURLConnection 使用详解
- 16暑期补TC计划
- RabbitMQ学习(三)之发布/订阅(java)
- 判断手机操作系统
- Java annotation 自定义注释@interface的用法
- dynamic - How to angular 2 dynamic tabs with user click chosen components