AOJ0064 Secret Number

問題リンク Secret Number

  • 解法

英数字ごちゃまぜの文字列から数字をピックアップして和を求める問題です。
先頭からサーチをかけて、数字を見つけたら、数字が続く限り10倍して足しこんでを繰り返します。

  • ソース
import java.util.Scanner;

//Secret Number
public class AOJ0064 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int s = 0;
		while(sc.hasNext()){
			char[] m = sc.next().toCharArray();
			for(int i=0;i<m.length;i++){
				if(Character.isDigit(m[i])){
					int x = m[i++]-'0';
					while(i<m.length&&Character.isDigit(m[i])){
						x*=10;
						x+=m[i++]-'0';
					}
					s+=x;
				}
			}
		}
		System.out.println(s);
	}
}