凑算式

题目


这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。(比赛时,题目有改动)
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?
29

思路

代码

import java.util.*;
public class Main{
    static int count=0;
    static void swap(char[]array,int a,int b){
        char temp=array[a];
        array[a]=array[b];
        array[b]=temp;
    }
    static boolean check(char[]str){
        double[]num=new double[10];
        for(int i=0;i<9;i++)
            num[i]=str[i]-'0';
        if(num[0]+num[1]*1.0/num[2]+(num[3]*100+num[4]*10+num[5])*1.0/(num[6]*100+num[7]*10+num[8])==10)
            return true;
        return false;
    }
    static void permutation(char[]str,int a,int length){//排列
        if(a==length){
            if(check(str)){
                count++;
//              System.out.println(String.valueOf(str));//打印所有可能的解法
            }
        }else{
            for(int i=a;i<=length;i++){
                swap(str,i,a);
                permutation(str,a+1,length);
                swap(str,i,a);
            }
        }
    }
    public static void main(String[] args){
         char[]str="123456789".toCharArray();
         permutation(str,0,8);//str.length-1(8)
         System.out.println(count);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值