へんてこのブログ

日々気づいたことや、最近やっていることを書いています

AOJ Volume0-0064

Secret Number
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0064

解法:全部読み込んでから解析
cin > sじゃダメだった、getline(sin,s)じゃないとダメ

#include<iostream>
#include<string>
#include <sstream>

using namespace std;

bool hoge(char s) {
	for (int i=0; i < 10; i++) {
		char h = (char) i + 48;
		if(s == h) return true;
	}
	return false;
}
int main() {
	string s,ss = "";
	while (getline(cin,s)) {
		ss += s;
	}	
	int result = 0;
	for (int i=0; i < ss.size(); i++) {
		string h = "";
		if (hoge(ss[i])) {
			h += ss[i];
			i++;
			while (hoge(ss[i]) && i < ss.size()) {
				h += ss[i];
				i++;
			}
		}
		if (h != "") {
			stringstream sss;
			int n;
			sss << h;
			sss >> n;
			result += n;
		}
	}
	
	cout << result << endl;

}