【思维】Codeforces 1030 C. Vasya and Golden Ticket

版权声明:转载标注来源喔~ https://blog.csdn.net/iroy33/article/details/89929008

题意:给你一组数字,问是否能把它分成连续的几段,每段的和相同

思路:先求和sum,对sum的每个约数i,检查数字是否能分成i段,每段的和为k

卑微小姚,思维布星,坑坑必踩

test3 是全0的情况,一定可以,要特判

test5 是00200200 ,原先的代码我只要和到了k,就把累加值改成0,到j=n时,tmp!=k 就判为错误。。之后加了个条件,如果累加和为0也是对的

test54 是 3 111, 应该是卡的全部数字相同。。。

#include<iostream>
using namespace std;
int n;
char a[110];
int main()
{
    cin>>n;
    int sum=0;
    for(int i=1;i<=n;++i)
    {
        cin>>a[i];
        sum+=a[i]-'0';
    }
    if(!sum)
    {
        cout<<"YES"<<endl;
        return 0;
    }
    for(int i=2;i<=sum;++i)
    {
        if(sum%i) continue;
        int k=sum/i;            //分为i组,每组的和为k
        int tmp=0;
        //cout<<"K:"<<k<<endl;
        for(int j=1;j<=n;++j)
        {
            tmp+=a[j]-'0';
            //cout<<j<<' '<<tmp<<endl;
            if(j==n)
            {
                if(tmp==k||tmp==0)
                {
                    cout<<"YES"<<endl;
                    return 0;
                }

            }
            else
            {
                if(tmp==k)
                {
                    tmp=0;
                }
                else if(tmp>k)
                    break;
            }

        }
    }
    cout<<"NO"<<endl;
    return 0;

}

猜你喜欢

转载自blog.csdn.net/iroy33/article/details/89929008