Java 043. 子串可整除性

  1. 子串可整除性
    数字 1406357289 由 0 至 9 的各位数构成,因此是一个 0 至 9 的全数字,除此之外,它还有
    一个有趣的子串可整除性质。设 d1 表示第一位数,d2 表示第二位数等等,可以发现:
    在这里插入图片描述
    求所有满足这个性质的 0 至 9 的全数字之和。
    答案:16695334890
    (满足条件的全数字有 6 个:1406357289, 1430952867, 1460357289, 4106357289, 4130952867,
    4160357289)
    代码如下:
public static int[] array=new int[]{2,3,5,7,11,13,17};
    public static boolean complete(String s){
        char[] temp=s.toCharArray();
        Arrays.sort(temp);
        if(new String(temp).equals("0123456789")) return true;
        else return false;
    }
    public static boolean div(String s){
        int t1,t2,t3,m;
        for(int i=1;i<=7;i++){
            t1=s.charAt(i)-'0';
            t2=s.charAt(i+1)-'0';
            t3=s.charAt(i+2)-'0';
            m=t1*100+t2*10+t3;
            if(m%array[i-1]!=0) return false;
        }
        return true;
    }
    public static long num(){
        long l=0;
        for(long i=9876543201l;i>=1023456789;i--){
            String s=Long.toString(i);
            if(complete(s)&&div(s)) l+=i;
        }
        return l;
    }
    public static void main(String[] args) {
        System.out.println(num());
    }

程序运行结果:
在这里插入图片描述

运行时间太长了,也许应该去了解一下排列组合了
注意:字符数组不能直接和字符串用equals比较,会出错

第二种方法:将10个数字进行排列(递归回溯,效率增快了不少)
代码如下:

public static int[] array=new int[]{2,3,5,7,11,13,17};
    public static int[] nums = {0, 1, 2, 3,4,5,6,7,8,9};
    public static ArrayList<ArrayList<Integer>> result = new ArrayList<>();

    public static void permuation(ArrayList<Integer> inner) {
        if (inner.size() == nums.length) {
            result.add(new ArrayList<>(inner));
            return;
        }

        for(int i=0; i<nums.length; i++) {

            if (inner.contains(nums[i])) {
                continue;
            }
            inner.add(nums[i]);
//            System.out.println(i+"A"+nums[i]);
//            System.out.println(inner);
            permuation(inner);
            inner.remove(inner.size()-1);
        }
    }

    public static boolean div(String s){
        int t1,t2,t3,m;
        for(int i=1;i<=7;i++){
            t1=s.charAt(i)-'0';
            t2=s.charAt(i+1)-'0';
            t3=s.charAt(i+2)-'0';
            m=t1*100+t2*10+t3;
            if(m%array[i-1]!=0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        ArrayList<Integer> inner = new ArrayList<>();
        String s="";
        long l=0l;
        permuation(inner);
        for(ArrayList<Integer> each: result) {
            s="";
            if(each.get(0)==0) continue;
            for(int i=0;i< each.size();i++){
                s+=Integer.toString(each.get(i));
//                System.out.println(s);
//                break;
            }
            if(div(s))
                l+=Long.parseLong(s);
//            System.out.println(s);
        }
        System.out.println(l);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Java后台实验三的代码实现,包含了找出子串在父串首次出现的位置、子串在父串出现的所有位置、将子串在父串的字符替换成新串等功能: ```java public class StringUtil { /** * 找出子串在父串首次出现的位置 * @param parent 父串 * @param child 子串 * @return 子串在父串中首次出现的位置,若不存在则返回-1 */ public static int indexOf(String parent, String child) { if (parent == null || child == null || child.length() > parent.length()) { return -1; } int n = parent.length(), m = child.length(); for (int i = 0; i <= n - m; i++) { boolean flag = true; for (int j = 0; j < m; j++) { if (parent.charAt(i + j) != child.charAt(j)) { flag = false; break; } } if (flag) { return i; } } return -1; } /** * 子串在父串出现的所有位置 * @param parent 父串 * @param child 子串 * @return 子串在父串中出现的所有位置 */ public static List<Integer> allIndexOf(String parent, String child) { List<Integer> list = new ArrayList<>(); if (parent == null || child == null || child.length() > parent.length()) { return list; } int n = parent.length(), m = child.length(), i = 0; while (i <= n - m) { boolean flag = true; for (int j = 0; j < m; j++) { if (parent.charAt(i + j) != child.charAt(j)) { flag = false; break; } } if (flag) { list.add(i); i += m; } else { i++; } } return list; } /** * 将子串在父串的字符替换成新串 * @param parent 父串 * @param child 子串 * @param newStr 新串 * @return 替换后的字符串 */ public static String replace(String parent, String child, String newStr) { if (parent == null || child == null || newStr == null) { return parent; } int n = parent.length(), m = child.length(), i = 0; StringBuilder sb = new StringBuilder(); while (i <= n - m) { boolean flag = true; for (int j = 0; j < m; j++) { if (parent.charAt(i + j) != child.charAt(j)) { flag = false; break; } } if (flag) { sb.append(newStr); i += m; } else { sb.append(parent.charAt(i)); i++; } } sb.append(parent.substring(i)); return sb.toString(); } } ``` 下面是测试类的代码: ```java public class StringUtilTest { public static void main(String[] args) { String parent = "hello world, world is beautiful, world"; String child = "world"; String newStr = "Java"; int index = StringUtil.indexOf(parent, child); System.out.println("子串在父串中首次出现的位置:" + index); List<Integer> list = StringUtil.allIndexOf(parent, child); System.out.println("子串在父串中出现的所有位置:" + list); String replaced = StringUtil.replace(parent, child, newStr); System.out.println("将子串在父串的字符替换成新串后:" + replaced); } } ``` 运行结果如下: ``` 子串在父串中首次出现的位置:6 子串在父串中出现的所有位置:[6, 18, 26] 将子串在父串的字符替换成新串后:hello Java, Java is beautiful, Java ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值