Codeforces 1005D:Polycarp and Div 3

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

 

题意

 给出个字符串(全是数字),把这个字符串换分成一些子串,最多能划分多少个能够被3整除的子串(字符串去掉前导零,0能被3整除)

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
char ch[maxn];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin>>ch;
	int l=strlen(ch);
	int ans=0;
	int sum=0;
	int x=0;
	int res;
	for(int i=0;i<l;i++)
	{
		res=(ch[i]-'0')%3;
		sum+=res;
		x++;
		if(res==0||sum%3==0||x==3)
		{
			ans++;
			sum=0;
			x=0;
		}
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wang_123_zy/article/details/81590380