广场自由字,以Java的 [英] Square Free Word in Java

查看:109
本文介绍了广场自由字,以Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卡上创建一个程序来解决一个问题的一类。我有一个主要的方法和在协同工作来解决这个问题的一个次要测试方法,但我不能得到解决时,有一个变化的工作。
问题是确保一个字广场免费,这里是从问题的摘录:


  

有关这一部分,实现一个称之为isSquareFree方法,该方法作为输入(基准)的字符阵列。你可以假设数组中的元素都是小写字母。 (换句话说,你不必担心这样的问题:为Z相同的字母为Z'),你的方法应该测试,如果给定字符输入数组是免费的平方。如果是,该方法应该打印一条消息指出,否则就应该打印一条消息,说明世界上没有方免费的,其中方子字开始和什么是子字。例如,如果给定数组包含zatabracabrac方法的话应该打印:这个词,zatabracabrac,是免费的不是方形的,因为它的子字,abrac两次起始于单词的4位


下面是当前code我有,它工作在有直接相邻的重复字符的情况,但我不确定如何继续检查是否有多个重复的字符(ABAB例如)我也不是知道如何打印出的重复子字。

 公共静态无效的主要(字串[] args){    //(a)部分主力    扫描仪键盘=新的扫描仪(System.in);    的System.out.println(********);
    的System.out.println(第(一));
    的System.out.println(********);    做{
        的System.out.println(输入一个单词,然后preSS输入:);
        字符串str = keyboard.next();
        的char []字= str.toCharArray();        isSquareFree(字);
        的System.out.println(你要测试的另一种说法是preSS y或无的另一个重要?);    }而(keyboard.next()的charAt(0)=='Y');
}公共静态无效isSquareFree(的char []字){
    INT平方英尺= 0;
    的for(int i = 0; I< word.length;我++){
        对于(INT J = 0; J< word.length-1; ​​J ++){
            如果(字[J] ==字[J + 1]){
                平方英尺= 1;
                J = word.length;
            }
            其他{
                平方英尺= 2;
            }
        }
    }
    如果(平方英尺== 1){
        的System.out.println();
        的System.out.println(不无平方);
    }
    其他{
        的System.out.println();
        的System.out.println(自由广场);
    }
}}

我还想补充一点,我不能使用数组类这个问题,我也不是允许使用字符串,我不能改变的主要方法,而不是我可以改变输入我的另一种方法


解决方案

  1. 要查看是否字符序列重复,对于给定的序列长度(例如, N 的),你将取代你的如果有一个循环,比较字[J + X] 字[J + N + X] 为每个值的 X 介于0和 N 的;而只考虑他们同样的,如果所有的 N 的匹配。因此,你需要遍历这些的 N 的为值x ;如果您需要考虑的不同值的 N 的,那么你需要另一个循环要经过这些。

    这是不是从你的code清楚你用的是什么 I 的,但如果是重复部分的长度(我打过电话的 N 的),那么你只需要考虑值高达字长度的一半(或者没有足够的空间重蹈覆辙)。


  2. 要打印出一分的话,你可以打印出来,以便每个字母(使用打印而不是的println


I'm stuck on creating a program to solve a question for a class. I have a main method and a secondary testing method that are working in conjunction to solve this problem, however I can't get the solution to work when there's a change. The problem is making sure a word is square free, here's an excerpt from the problem:

For this part, implement a method called isSquareFree that takes as input (a reference to ) an array of characters. You may assume that the elements of the array are all lower case letters. (In other words, you do not need to worry about a question like: "is Z the same letter as z?") Your method should test if the given input array of characters is square-free. If it is, the method should print a message stating that, otherwise it should print a message stating that the world is not square-free, where the square subword starts and what that subword is. For example, if the given array contained the word zatabracabrac the method should print: The word, zatabracabrac, is not square free, since it has subword, abrac twice starting at position 4 of the word.

Below is the current code I have, it works in the case that there is a repeating character directly next to each other, but I'm unsure of how to continue to check if there is multiple repeating characters (abab for example) nor am I sure how to print out the repeating subword.

public static void main(String[] args) {

    // part (a) of the main

    Scanner keyboard = new Scanner(System.in);

    System.out.println("***************************");
    System.out.println("        Part (a)");
    System.out.println("***************************");

    do{
        System.out.println("Enter a word and then press enter:");
        String str=keyboard.next();
        char[] word = str.toCharArray();

        isSquareFree(word);
        System.out.println("Do you want to test another word? Press y for yes, or another key for no");

    }while(keyboard.next().charAt(0)=='y');


}

public static void isSquareFree(char[] word){
    int sqf = 0;
    for(int i=0; i<word.length; i++){
        for(int j=0; j<word.length-1;j++){
            if (word[j] == word[j+1]){
                sqf = 1;
                j = word.length;
            }
            else{
                sqf = 2;
            }
        }
    }
    if (sqf == 1){
        System.out.println();
        System.out.println("Not Square Free");
    }
    else{
        System.out.println();
        System.out.println("Square Free");
    }
}}

I'd also like to add that I'm not allowed to use the arrays class for this question, nor am I allowed to use strings and I cannot change the main method, not can I change the input for my other method.

解决方案

  1. To see if a sequence of characters repeats, for a given sequence length (say, n), you would replace your if with a loop that compares word[j+x] with word[j+n+x] for each value of x between 0 and n; and only consider them the same if all n match. Thus, you'd need to loop over these n values for x; if you need to consider different values of n, then you'd need yet another loop to go through those.

    It isn't clear from your code what you are using i for, but if it is the length of the repeating part (what I've called n), then you'd only need to consider values up to half the length of word (or else there isn't room to repeat it).

  2. To print out a sub word, you could print out each individual letter in order (using print instead of println)

这篇关于广场自由字,以Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆