【问题标题】:How to determine if the sequence of array values is palindromic?如何确定数组值的序列是否是回文的?
【发布时间】:2017-03-03 04:56:20
【问题描述】:

编写一个程序,它将接受 10 个整数并将它们存储在一个数组中。程序应该测试数字序列是否是回文序列。这意味着它向前读取与向后读取相同。这里有两个例子:

请输入 10 个整数: [1|2|3|7|7|7|7|3|2|1]

序列是回文的。

请输入 10 个整数: [1|2|3|7|7|7|7|2|3|1]

序列不是回文的。

我的代码是:

import java.util.Scanner;
public class problem4 {
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please type 10 integers:");
        int[] num = new int[10];
        int[] reverse = new int[10];
        for (int a = 0; a < num.length; a++) {
            num[a] = scan.nextInt();
        }
        int b = 0;
        int c = 9;
        while (c != 0) {
            reverse[c] = num[b];
            c--;
            b++;
            if (c == 0) break;
        }
        if (num[0] == reverse[9] && num[1] == reverse[8] && num[2] == reverse[7] && num[3] == reverse[6] && num[4] == reverse[5])
            System.out.println("The sequence is palindromic.");
        else 
            System.out.println("The sequence is not palindromic.");
    }
}

但是没有构建消息,它总是说序列是回文的,无论我输入什么。

【问题讨论】:

  • 为什么不直接取值,将它们放在 one 数组中,然后从两端进行迭代,以确保前端和结束元素一路上相同?跨度>
  • 如果您没有使用数组,您可以将前 5 个压入堆栈,然后根据堆栈中的值 pop()-ed 检查接下来的 5 个。

标签: java arrays if-statement while-loop sequence


【解决方案1】:

您可以只创建一个简单的方法并传递数组来检查回文。

.......主要从这里开始

Scanner scan = new Scanner(System.in);
    System.out.println("Please type 10 integers:");
    int[] num = new int[10];
    int[] reverse = new int[10];
    for (int a = 0; a < num.length; a++) {
        num[a] = scan.nextInt();
    }
    System.out.println(palin(num));

.......主要在这里结束 和类中的方法

你基本上把数组分成两个 1 2 3 7 7 7 7 3 2 1

并检查front是否与back相同,first是起始索引,prev是最后一个索引。

public static String palin(int[] arr){
    int prev, first;
    boolean tr = true;
    for(first = 0, prev = arr.length-1; first < arr.length/2; prev--,first++){
        if(arr[first] != arr[prev]){
            tr = false;
            break;
        }
    }
    return tr ? "Is a palindromic":"Not palindromic";   
 }

大家一起

 import java.util.Scanner;


public class problem4 {

public static void main (String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please type 10 integers:");
    int[] num = new int[10];
    int[] reverse = new int[10];
    for (int a = 0; a < num.length; a++) {
        num[a] = scan.nextInt();
    }

   System.out.println(palin(num));
}   

    public static String palin(int[] arr){
    int prev, first;
    boolean tr = true;
    for(first = 0, prev = arr.length-1; first < arr.length/2; prev--,first++){
        if(arr[first] != arr[prev]){
            tr = false;
            break;
        }
    }
    return tr ? "Is a palindromic":"Not palindromic";   
 }

}

【讨论】:

  • 仅供参考:你基本上用first &lt; arr.length - 1检查了两次。
  • 我同时检查前后,首先在和 0 和 prev 从数组的末尾开始。就像使用内置迭代器进行上一个和下一个但不想那样,我希望他理解。 @ChiefTwoPencils
  • 是的,但是 prevfirst 相互交叉,因此除了第一个和最后一个元素之外的每个元素都会被检查两次。
  • @ChiefTwoPencils 是的,我知道了,感谢您的提示,它可以加快速度而无需重复。我更新了我的答案
【解决方案2】:

您不需要使用反转数组并与之比较元素。

在你的代码中试试这个:

public static void main (String[] args) {
   Scanner scan = new Scanner(System.in);
   System.out.println("Please type 10 integers:");
   int[] num = new int[10];
   int[] reverse = new int[10];
   for (int a = 0; a < num.length; a++) {
       num[a] = scan.nextInt();
    }

if (num[0] == num[9] && num[1] == num[8] && num[2] == num[7] 
     && num[3] == num[6] && num[4] == num[5])
        System.out.println("The sequence is palindromic.");
 else 
    System.out.println("The sequence is not palindromic.");

【讨论】:

    【解决方案3】:

    您可以通过迭代数组的一半来简化此操作(如果 num[a] 不等于 num[num.length - a - 1] 它不是回文)。类似的,

    Scanner scan = new Scanner(System.in);
    System.out.println("Please type 10 integers:");
    int[] num = new int[10];
    for (int a = 0; a < num.length; a++) {
        num[a] = scan.nextInt();
    }
    boolean isPalindrome = true;
    for (int a = 0; a <= num.length / 2; a++) {
        if (num[a] != num[num.length - a - 1]) {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome) {
        System.out.println("The sequence is palindromic.");
    } else {
        System.out.println("The sequence is not palindromic.");
    }
    

    我测试的(如指定的)喜欢

    Please type 10 integers:
    1 2 3 7 7 7 7 3 2 1
    The sequence is palindromic.
    

    Please type 10 integers:
    1 2 3 7 7 7 7 2 3 1
    The sequence is not palindromic.
    

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 1970-01-01
      • 2016-01-29
      相关资源
      最近更新 更多