3539 - 填算式(蓝桥杯真题)

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

    看这个算式:
    ☆☆☆ + ☆☆☆ = ☆☆☆
    如果每个五角星代表 1 ~ 9 的不同的数字。
    这个算式有多少种可能的正确填写方法?
    173 + 286 = 459
    295 + 173 = 468
    173 + 295 = 468
    183 + 492 = 675
    以上都是正确的填写法!
    注意:
    111 + 222 = 333 是错误的填写法!
    因为每个数字必须是不同的!
    也就是说:1~9中的所有数字,每个必须出现且仅出现一次!
    注意:
    不包括数字“0”!
    注意:
    满足加法交换率的式子算两种不同的答案。
    所以答案肯定是个偶数!

题目输入

 

题目输出

输入/输出样例

输入格式


                        

输出格式


                        

C++解答

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int p[9]={1,2,3,4,5,6,7,8,9};
	int cnt=0;
	do
	{
		if((p[0]*100+p[1]*10+p[2]+p[3]*100+p[4]*10+p[5])==(p[6]*100+p[7]*10+p[8]))
			cnt++;	
	}while(next_permutation(p,p+9));
	cout<<cnt;
	return 0;
}

Java解答


public class Main {
public static void main(String []args){
	String test;
	int count = 0;
	//System.out.println(f("456789123"));
	for(int i=300;i<1800;i++){
		for(int j=100;j<1000&&j<i;j++){
			int t = i-j;
			test=i+""+""+j+""+t+"";
			//System.out.println(test);
			if(test.length()==9 &&f(test)){
				//System.out.println(test);
				count++;
			}
			test = "";
		}
	}
	System.out.println(count);
}
public static boolean f(String s){
	char a[]={'1','2','3','4','5','6','7','8','9'};
	for(int i=0;i<s.length();i++){
		for(int j=0;j<a.length;j++){
			if(a[j]=='o') continue;
			if(s.charAt(i)==a[j]) a[j]='o';
		}
	}
	for(int i=0;i<a.length;i++){
		if(a[i]!='o') return false;
	}
	return true;
}
}