【模拟与高精度】【JAVA】P1591 阶乘数码

在这里插入图片描述

思路:处理大数+求阶乘+字符串查找
处理大数可以用java的BigInteger封装类
求阶乘直接用循环求,返回值要是字符串才行,避免数值越界
字符串查找比较的时候记得用equal

package LOQ.模拟与高精度;

import java.math.BigInteger;
import java.util.Scanner;

//阶乘数码
public class P1591 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        String[] strs = new String[t];
        //输入数据
        sc.nextLine();
        for (int i = 0; i < t; i++) {
            strs[i] = sc.nextLine();
        }
        //求每行的阶乘
        int count;
        String a = "";
        for(int i=0;i<t;i++) {
            //重置计数器
            count=0;
            String[] split = strs[i].split(" ");
            //获取到求阶乘的最终值
            BigInteger n = new BigInteger(split[0]);
            //调用求阶乘方法
            String resStr = getFactor(n);
            //计算数码出现阶乘结果中的次数
            for(int j=0;j<resStr.length();j++) {
                String[] split1 = resStr.split("");
                if(split1[j].equals(split[1])) {
                    count++;
                }
            }
            //输出结果
            System.out.println(count);
        }
    }
    //求阶乘方法
    public static String getFactor(BigInteger n) {
        int foctorMax = Integer.parseInt(n.toString());
        for(int i=1;i<foctorMax;i++) {
            n=n.multiply(new BigInteger(String.valueOf(i)));
        }
        return n.toString();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值