【codeforces1058】Vasya and Golden Ticket 枚举+暴力+模拟

#点击传送

题目描述

Recently Vasya found a golden ticket — a sequence which consists of nn digits a1a2…ana1a2…an. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segments with equal sums. For example, ticket 350178350178 is lucky since it can be divided into three segments 350350, 1717 and 88: 3+5+0=1+7=83+5+0=1+7=8. Note that each digit of sequence should belong to exactly one segment.

Help Vasya! Tell him if the golden ticket he found is lucky or not.

题目大意

判断能否就是将一个数字串,分成连续的若干段,使其每一段的和相等

分析

也没什么好讲的,数据很小,直接上暴力,枚举每一段的和,判断是否成立。
注意:!!!要判断0的情况,这是一个坑!!!!

AC代码

#include <bits/stdc++.h>

using namespace std;

char ch[105];
int a[105];

int main() {
    int len; scanf("%d",&len);
    scanf("%s",ch);
    for (int i=1;i<=len;i++) a[i]=ch[i-1]-'0';
    int fg=0;
    for (int i=0;i<=900;i++) {
        int sum=0,cnt=0;
        for (int j=1;j<=len;j++) {
            sum+=a[j];
            if (sum==i) sum=0,cnt++; else if (sum>i) break;
        }
        if (sum==0 && cnt>=2) fg=1;
    }
    if (fg) printf("YES\n"); else printf("NO\n");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Dawn-Star/p/9715985.html