[华为机试]求出数组中全部奇数之和以及所有偶数之和

[华为机试]求出数组中所有奇数之和以及所有偶数之和

[华为机试]求出数组中全部奇数之和以及所有偶数之和

代码:

#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[i] >= '0' && str[i] <= '9')
			{
				count++;
			}
		}
		if (count != str.size())
		{
			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;
}


1楼th_mail3小时前
应该要把负数的字符考虑进去吧