首页 > 技术文章 > 练习

jiehao-yu 2021-10-23 20:59 原文

/**
 * @author JH_Y
 * @version 1.0
 */
public class Test {


    public static void main(String[] args) {
        System.out.println(t1(8));
        String s = "abcdefabc";
        String s1 = "abc";
        System.out.println(countStr(s, s1));
    }


    /*
    如果输入的n是7的倍数,直接返回 n
     如果不是,就输入附近最近的7的倍数
     比如 输入8 输出7 ,输入 20 输出21
     */
    public static int t1(int n) {
        if (n % 7 == 0) {
            return n;
        } else if (n % 7 >= 3) {
            return n - n % 7 + 7;
        } else if (n % 7 < 3) {
            return n - n % 7;
        }
        return 0;
    }


    /*
    子串在母串中出现的次数
    例如:“abc”在“abcdefabc”出现了两次
     */
    public static int countStr(String str, String substr) {
        int count = 0;
        String s1 = "";
        for (int i = 0; i <= str.length() - substr.length(); i++) {
            s1 = str.substring(i, i + substr.length());
            if (substr.contains(s1)) {
                count++;
            }
        }
        return count;
    }
}

推荐阅读