如何确定数组值序列是否是回文?

时间:2016-10-21 01:56:23

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

编写一个程序,它将采用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.");
    }
}

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

3 个答案:

答案 0 :(得分:1)

你可以创建一个简单的方法并传递数组来检查palindrom。

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

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&lt; ----&gt; 7 7 3 2 1

并检查前面是否与后面相同,首先是起始索引,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";   
 }

}

答案 1 :(得分:0)

您可以通过迭代数组的一半来简化此操作(如果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.

答案 2 :(得分:0)

您不需要使用反转数组并将元素与它进行比较。

在您的代码中尝试此操作:

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.");