繁体   English   中英

程序找到重复n个字符的字符串中的'a'数?

[英]Program to find the number of 'a' in a string that is repeated n characters?

我正在编写一个程序来查找重复给定字符串中的'a'数。 例如,调用findAmountA(“aba”,7)意味着它找到重复7个字符的字符串“aba”中的“a”的数量。 因此“abaabaa”是最后一个字符串,因此该调用将返回5。

如果没有实际使字符串成为7个字符(因此要求1,000,000个字符的时间不会那么长),我将如何使用数学来完成此任务? 我不能比这更进一步,因为我一直试图解决这个问题。

请记住,我是一名初学者Java程序员(学生),不想使用我在高中时不会学习的任何高级/花哨语法。 谢谢!

public class AInString {
    public static void main(String[] args) {
        boolean a = findAmountA("aba", 10) == 7;
        boolean b = findAmountA("a", 100) == 100;
        boolean c = findAmountA("abca", 10) == 5;
        boolean d = findAmountA("", 10) == 0;
        boolean e = findAmountA("abcaa", 1000000) == 600000;
        boolean f = findAmountA("abc", 0) == 0;
        boolean g = findAmountA("bcd", 10) == 0;
        System.out.println(a && b && c && d && e && f && g);
    }
    public static int findAmountA(String word, int n) {
        String s = word;
        if(s.length() == 0 || aInWord(word) == 0) {
            return 0;
        }else {
            int a = (aInWord(s));
            return a;
        }
    }
    public static int aInWord(String word) {
        String s = word;
        int aInWord = 0;
        for(int i = 0; i < word.length(); i++) {
            if(s.charAt(i) == 'a') {
                aInWord++;
            }
        }
        return aInWord;
    }

}

假设您的短字符串w有N个副本'a' 然后结果字符串将由w的K个副本组成,后跟可能的空“尾部”字符串。

K的值可以通过将目标串中的'a'的数量除以N来确定。然后,“尾部”中的“ 'a'的数量t将等于除法的余数。 现在你可以打印w副本,然后打印包含t 'a'的最短前缀'w'

现在您已经计算了字符串word char a的出现次数,您可以计算字符串扩展n字符中char的出现次数:

return n / word.length() * aInWord(word) + aInWord(word.substring(0, n % word.length()));

n / word.length()给出适合n的字符串的完整重复次数。 通过的计数乘以这个aInWord(word)给出的计数a在重复word适合干净成n

其余的是发现在的子串的重复的数量的问题word不干净适合n使用%模运算符以找到局部子串的大小(如果有的话)。 将两个计数加在一起会产生扩展字符串中出现的总数。

这是一个干净的版本,它避免了重复变量,额外的条件和一般化方法来最大化可重用性:

class Main {
    public static void main(String[] args) {
        assert findAmount("aba", 10, "a") == 7;
        assert findAmount("a", 100, "a") == 100;
        assert findAmount("abca", 10, "a") == 5;
        assert findAmount("", 10, "a") == 0;
        assert findAmount("abcaa", 1000000, "a") == 600000;
        assert findAmount("abc", 0, "a") == 0;
        assert findAmount("bcd", 10, "a") == 0;
        System.out.println("tests passed");
    }

    public static int findAmount(String word, int n, String target) {
        if (word.length() == 0) {
            return 0;
        }

        return n / word.length() * count(target, word) + 
               count(target, word.substring(0, n % word.length()));
    }

    public static int count(String target, String s) {
        return s.length() - s.replace(target, "").length();
    }
}

试试吧!

将目标长度除以输入长度:例如:

7 / 3 = 2 remainder 1

2您将使用的整个输入字符串的“完整副本”数。 因此,在整个字符串中找到“a”的数量,乘以2。

您将获取输入的前1个字符以构成7个字符的剩余部分。 计算该子字符串中“a”的数量。

只需将这两个数字加在一起即可

int total = count(input, "a") * targetLength / input.length()
          + count(input.substring(0, targetLength % input.length()), "a");

其中count(input, c)是一些方法来计数的发生次数cinput

我对你的代码做了一些修改,看看:

public static void main(String[] args) {
    int a = findAmountA("aba", 10); // 7
    int b = findAmountA("a", 100); // 100;
    int c = findAmountA("abca", 10); //5;
    int d = findAmountA("", 10); //0;
    int f = findAmountA("abc", 0); //0;
    int g = findAmountA("bcd", 10); //0;
    System.out.println(a + " " + b + " " + c + " " + d + " " + f + " " + g);
}

public static int findAmountA(String word, int n) {
    if (word.length() < n) {
        for (int i=0; i<word.length(); i++) {
            while (word.length() < n) {
                word = word + word.charAt(i);
                break;
            }
        }
    } else if (word.length() > n) {
        for (int i=0; i<word.length(); i++) {
            word = word.substring(0, n);
        }
    } else {
        return aInWord(word);
    }
    return aInWord(word);
}

public static int aInWord(String word) {
    String s = word;
    int aInWord = 0;
    for(int i = 0; i < word.length(); i++) {
        if(s.charAt(i) == 'a') {
            aInWord++;
        }
    }

谢谢大家的帮助,使用子串我找到了答案:

public class AInString {
    public static void main(String[] args) {
        boolean a = findAmountA("aba", 10) == 7;
        boolean b = findAmountA("a", 100) == 100;
        boolean c = findAmountA("abca", 10) == 5;
        boolean d = findAmountA("", 10) == 0;
        boolean e = findAmountA("abcaa", 1000000) == 600000;
        boolean f = findAmountA("abc", 0) == 0;
        boolean g = findAmountA("bcd", 10) == 0;
        System.out.println(a && b && c && d && e && f && g);
    }
    public static int findAmountA(String word, int n) {
        String s = word;
        if(s.length() == 0 || aInWord(s) == 0) {
            return 0;
        }else {
            int a = aInWord(s)*(n/s.length());
            int b = n % s.length();
            return a + aInWord(s.substring(0, b));
        }
    }
    public static int aInWord(String word) {
        String s = word;
        int aInWord = 0;
        for(int i = 0; i < word.length(); i++) {
            if(s.charAt(i) == 'a') {
                aInWord++;
            }
        }
        return aInWord;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM