您的位置:首页 > 其它

A very hard Aoshu problem

2017-11-06 10:04 281 查看

A very hard Aoshu problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1705    Accepted Submission(s): 1174


[align=left]Problem Description[/align]

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.

 

[align=left]Input[/align]

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".

 

[align=left]Output[/align]

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

 

[align=left]Sample Input[/align]

1212
12345666
1235
END

 

[align=left]Sample Output[/align]

2
2
0

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cnt,len,lsum,rsum;
int an[30],lans[30],rans[30];
void rdfs(int left,int right,int flag,int rtail)
{
if(flag==1)
{
rtail++;
rans[rtail]=an[left];
}
else if(flag==2)
{
rans[rtail]=rans[rtail]*10+an[left];
if(rans[rtail]>=1e8)
return ;
}
else
rans[rtail]=an[left];
if(left==right)
{
rsum=0;
for(int i=0;i<=rtail;i++)
{
rsum+=rans[i];
if(rsum>1e8)
return ;
}
if(lsum==rsum)
cnt++;
return ;
}
rdfs(left+1,right,1,rtail);
rdfs(left+1,right,2,rtail);
}
void ldfs(int left,int right,int flag,int ltail)
{
if(flag==1)
{
ltail++;
lans[ltail]=an[left];
}
else if(flag==2)
{
lans[ltail]=lans[ltail]*10+an[left];
if(lans[ltail]>=1e8)
return ;
}
else
lans[ltail]=an[left];
if(left==right)
{
lsum=0;
for(int i=0;i<=ltail;i++)
{
lsum+=lans[i];
if(lsum>1e8)
return ;
}
rdfs(right+1,len-1,0,0);
return ;
}
ldfs(left+1,right,1,ltail);
ldfs(left+1,right,2,ltail);
}
int main()
{
while(1)
{
char ch[30]={0};
memset(an,0,sizeof(an));
gets(ch);
len=strlen(ch);
if(strcmp(ch,"END")==0)
break;
for(int i=0;ch[i]!='\0';i++)
{
an[i]=ch[i]-'0';
}
cnt=0;
for(int i=1;i<len;i++)
{
memset(lans,0,sizeof(lans));
memset(rans,0,sizeof(rans));
ldfs(0,i-1,0,0);
}
printf("%d\n",cnt);
}

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: