[华为机试]求出数组中全部奇数之和以及全部偶数之和
2016-01-14 08:21
309 查看
代码:
#include <iostream> #include <sstream> #include <fstream> #include <string> int main() { int num[100]; int odd = 0, even = 0; std::string str; getline(std::cin, str); std::stringstream s(str); int j = 0; while(getline(s, str, ',')) { //考虑了字符串中有非数字的情况(不考虑貌似也对了) int count = 0; for (int i = 0; i < str.size(); i++) { if(str[0] == '-') { neg = true; } if (str[i] >= '0' && str[i] <= '9') { count++; } } if (count != str.size() && !neg) { continue; } if( neg && count != str.size() - 1 ) { neg = false; continue; } num[j] = atoi(str.c_str()); if (num[j] % 2 == 0) { even += num[j]; j++; } else { odd += num[j]; j++; } } std::cout<<odd <<","<< even; }
相关文章推荐
- 使用iconfont时进行拼接字符图标
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- (笔记)AES加密在线计算工具
- Win10系统中PIN码登录无法正常使用该怎么办?
- Win10预览版11099已知问题汇总 解决文件传输进度无法显示
- 接下来的事
- 小娜学法(1):宪法宣誓
- POJ 1111 - Image Perimeters
- SQL Server 字符串拼接与拆分 string varchar Split and Join
- 系统找不到指定的路径
- 编写好代码的10条戒律
- HBase is still a baby
- 逐层宽度优先搜索(BFS)二叉树各种解法
- Python list 排序
- 二十九(Sparse coding练习)
- 二十八(使用BP算法思想求解Sparse coding中矩阵范数导数)
- 1602: [Usaco2008 Oct]牧场行走|Lca
- 二十七(Sparse coding中关于矩阵的范数求导)
- STL vector提高效率注意事项与技巧
- 二十六(Sparse coding简单理解)