凑算式
时间: 1ms 内存:128M
描述:
B DEF
A + --- + ------- = 10
C GHI这个算式中A~I代表0~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
问:这个算式一共有多少种解法?
输入:
无
输出:
输出能使这个算式成立的解法数量。
示例输入:
示例输出:
提示:
参考答案(内存最优[0]):
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
int times=0;
int Judge(char a[])
{
double b1,b2,b3,b4,b5;
b1=a[0]-'0';
b2=a[1]-'0';
b3=a[2]-'0';
b4=(a[3]-'0')*100+(a[4]-'0')*10+(a[5]-'0');
b5=(a[6]-'0')*100+(a[7]-'0')*10+(a[8]-'0');
if((b1+b2/b3+b4/b5)==10)
{
times++;
//cout<<b1<<" "<<b2<<" "<<b3<<" "<<b4<<" "<<b5<<endl;;
}
}
int main(){
char a[10]="123456789";
Judge(a);
while(next_permutation(a,a+9))
{
Judge(a);
}
cout<<times;
}
参考答案(时间最优[0]):
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
int times=0;
int Judge(char a[])
{
double b1,b2,b3,b4,b5;
b1=a[0]-'0';
b2=a[1]-'0';
b3=a[2]-'0';
b4=(a[3]-'0')*100+(a[4]-'0')*10+(a[5]-'0');
b5=(a[6]-'0')*100+(a[7]-'0')*10+(a[8]-'0');
if((b1+b2/b3+b4/b5)==10)
{
times++;
//cout<<b1<<" "<<b2<<" "<<b3<<" "<<b4<<" "<<b5<<endl;;
}
}
int main(){
char a[10]="123456789";
Judge(a);
while(next_permutation(a,a+9))
{
Judge(a);
}
cout<<times;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。