http://codeforces.com/contest/1005/problem/D
解题思路:
1.能被3整除的单个数字(0,3,6,9……)
2.对3取模的数字做两个计数器,当余数1的个数或余数2的个数出现连续三个时
3.当余数1和2的个数各有一个
4.满足以上三种情况则令目标计数器+1,然后把两个余数计数器清零重新计算
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int cnt = 0,x1 = 0,x2 = 0;
for(int i = 0;i < str.length();i++) {
if((str.charAt(i) - 48) % 3 == 0) {
cnt++;
x1 = x2 = 0;
} else if((str.charAt(i) - 48) % 3 == 1) {
x1++;
} else if((str.charAt(i) - 48) % 3 == 2) {
x2++;
}
if(x1 == 3 || x2 == 3 || (x1 >= 1 && x2 >= 1)) {
cnt++;
x1 = x2 = 0;
}
}
System.out.println(cnt);
}
}