您的位置:首页 > 大数据 > 人工智能

D - A very hard Aoshu problem----(2015 summer training #12(Team_Qualifying))

2015-08-14 15:43 459 查看
D - A very hard Aoshu problem
时限:1000MS 内存:32768KB 64位IO格式:%I64d
& %I64u

问题描述

Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his
students:

Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2".
Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.

输入

There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END".

输出

For each test case , output a integer in a line, indicating the number of equations you can get.

样例输入

1212
12345666
1235
END


样例输出

2
2
0


分析:队友太给力了,直接秒AC。

思路:列举所有的可能性,然后逐一比较。

CODE:

#include <bits/stdc++.h>
using namespace std;

int t[20][20],ans,mid,len;
char a[20];
void dfs2(int i,long long data,long long pre){
if (i>len) {
if (data==pre) {
ans++;
return;
}
}
for (int k=i; k<=len; k++) {
dfs2(k+1, data+t[i][k], pre);
}
return;
}
void dfs1(int i,long long data){
if (i>mid) {
dfs2(mid+1,0,data);
return;
}
for (int k=i; k<=mid; k++) {
dfs1(k+1, data+t[i][k]);
}
return;
}
int main(){
while (scanf("%s",a+1)) {
if (!strcmp(a+1,"END")) {
break;
}
len=strlen(a+1);
for (int i=1; i<=len; i++) {
for (int j=i; j<=len; j++) {
t[i][j]=0;
for (int k=i; k<=j; k++) {
t[i][j]=t[i][j]*10+(a[k]-'0');
}
cout<<t[i][j]<<endl;
}
}
ans=0;
for (mid=1; mid<len; mid++) {
dfs1(1, 0);
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: