如何判断一串单词是否符合斐波那契模式

How to find out if a string of words fall in fibonacci pattern or not

问题:验证作为输入给出的字符串,单词的长度是否属于斐波那契模式,即单词的长度是它前面两个单词的长度之和。

一个单词只由字母组成。例如。 ra!hul 是由 !.

分隔的 2 个单词

输入可以包含任意数量的单词。所有输入必须至少有 3 个字。 例如 I AM, RON!! 遵循模式 (I, AM, RON),而 Is IT HERs!E(Is, IT , HERs, E) 没有,因为第 4 个元素的长度应该是 6。

输出应该是如果输入的所有单词都符合斐波那契模式,那么在检查所有单词后最后应该输出YES,如果它在任何部分都不属于斐波那契模式输入,它应该输出 NO,而无需进一步评估,此后也将结束 运行。

我的问题:我确实写了一个代码,它不断输出为NO,我不明白我的逻辑哪里出错了,但我觉得我在切换循环,做 modify/suggest ,替代和更好的方法太受欢迎了。这是我的代码:

public class Holla {
      static int j=-1,a,b,c;
       public static void main(String[] args) {
               Scanner s = new Scanner(System.in);
            String h=s.nextLine();
            int i=0,x=0;
            while(i<h.length())
            {
                if(Character.isLetter(h.charAt(i)))
                     x++;
                else
                {
                    while(x>0)
               out: {
                       j++;
                        switch (j) 
                        {
                            case 0:
                                a=x;
                                break;
                            case 1:
                                b=x;
                                break;
                            default:
                                c=x;
                        }
                        if((j==0)||(j==1))
                              break out;
                            if (c==(a+b))
                                {
                                   a=b;
                                    b=c;
                                    j=1;
                                }
                                else
                                {
                                    System.out.println("NO");
                                    System.exit(0);
                                }
                    }
                    x=0;
                }
                i++;
            }
           System.out.println("YES");
        }
    }

您可以更多地使用 Java 标准库。

首先,假设您输入了:String input.

为了让生活更轻松,让我们将其拆分为元素单词列表,每个单词由不同于普通字母的内容分隔:

String pattern = "^[a-zA-Z]";
String tokens[] = input.split(pattern);
Predicate<String> filterNonEmpty = a -> !a.isEmpty();
List<String> words =
    Arrays.asList(tokens).stream().filter(filterNonEmpty).collect(Collectors.toList());

好的,现在我们有了单词,所以是时候 运行 "Fibonacci checker"(也就是说,我们检查每个单词的长度是否等于前两个单词的长度之和个):

if (words.size() < 3) { .... } // handle edge case specified in question

// grab lengths of first & second word
int len1 = words.get(0).length();
int len2 = words.get(1).length();

// now iterate over remaining words
for (int i = 2; i < words.size; ++i) {
    String newWord = words.get(i);
    int newLen = newWord.length();
    if (newLen != len1 + len2) {
        // word at index [i] does not have "Fibonacci size"
        return false;
    } else {
        // everything good, we need to move the lengths
        len1 = len2;
        len2 = newLen;
    }
}
// all the words had "Fibonacci size"
return true;

顺便说一句。 请注意,此代码将 return true 用于长度为 1, 3, 4, 7, 11 的单词,这不是斐波那契数列(因为起始元素不是 0 & 1 / 1 & 1 ).