1.Description
给出一个长为 n ,由数字组成的字符串,问将这个字符串分为连续的一些组,使得每个组内的数字之和相等,最多可以分为多少组。
2.Example
输入
在第一行中给出字符串的长度 n,在第二行中给出一个长为 n ,由数字组成的字符串 s 。
(1 <= n <= 10^6)
输出
最多可以分的组数。
样例输入
5
31113
样例输出
3
3.Solution
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String str = sc.next();
int sum = 0;
for(int i=0;i<n;i++) {
//对字符串进行求和
sum += str.charAt(i)-'0';
}
for(int i=n;i>=2;i--) {
if(sum%i==0) {
boolean ok=true;//能否恰好分为i组
int ave = sum/i;//每组的和
int cur = 0;//当前组的和
for(int j=0;j<n;j++) {
cur += str.charAt(j)-'0';
if(cur>ave) {
ok = false;
}else if(cur==ave) {
cur=0;
}
}
if(ok) {
System.out.println(i);
break;
}
}
}
}
}