Codeforces 1005D Polycarp and Div 3

题目链接:http://codeforces.com/problemset/problem/1005/D

题意:给定一个只由数字组成的字符串,问最多能分割出多少个能被三整除的数字。字符串长度小于等于2e5

解题思路:

首先对一位数来说,能被3整除ans++;
两位数的话,也是能被三整除就ans++;
对于三位数的话,就说明前面两位数对三取余的情况就是(1,1),(2,2),第三位数再对三取余,0,1,2,然后三位数都满足被三整除。所以当三位数的时候一定就被3整除。

代码

#include <iostream>
#include<string.h>
#include<cstdio>
using namespace std;
char a[100005], b[100005];
int main()
{
	string a;
	cin >> a;
	int ans = 0;
	int tmp = 0;
	int i = 0;
	for ( i = 0; i < a.size(); i++ )
	{

		if ((a[i] - '0') % 3 == 0 )
		{
			tmp = 0;
			ans++;
			continue;
		}
		tmp *= 10;
		tmp += a[i] - '0';
		if (tmp % 3 == 0 || tmp > 100)
		{
			ans++;
			tmp = 0;
			continue;

		}
	}
	cout << ans << endl;
	return 0;
}

总结:

   遇到这种题的时候要多去找找规律。多去思考
posted @ 2018-07-30 09:36  IAMjm  阅读(160)  评论(0编辑  收藏  举报