gpt4 book ai didi

java - ACM 401-在线回教在线法官给出错误的答案

转载 作者:行者123 更新时间:2023-12-02 04:41:34 26 4
gpt4 key购买 nike

import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader stdin;
String str;

// TODO Auto-generated method stub
stdin = new BufferedReader(new InputStreamReader(System.in));

char[] stringArray ;

boolean isPalindrone, isMirrored;

while((str = stdin.readLine())!= null) {
//convert string into array using toCharArray() method of string class
stringArray = str.toCharArray();

isPalindrone = new Main().checkPalindromes(stringArray);
isMirrored = new Main().precheckMirroring(stringArray);

if(isPalindrone && isMirrored) {
System.out.println(str + " -- is a mirrored palindrome.");
} else if(isPalindrone) {
System.out.println(str + " -- is a regular palindrome.");
} else if(!isPalindrone && isMirrored) {
System.out.println(str + " -- is a mirrored string.");
} else {
System.out.println(str + " -- is not a palindrome.");
}

System.out.println();
}
}

/* precheckpalindronetest */
public boolean precheckMirroring(char stringArray[]) {
boolean pretest_ismirror = true;

char[] m_char = {'A' , 'E' , 'H', 'I' , 'J', 'L','M', 'O' ,'T','U' ,
'V','W' , 'X' , 'Y', 'Z' , '1', '2','3' , '5' , '8'
};

boolean match = false;
for(int i = 0 ; i < stringArray.length; ++i) {
match = false;
for(int j = 0 ; j < m_char.length; ++j) {
if(stringArray[i] == m_char[j]) {
match = true;
break;
}
}

if(match == false) {
pretest_ismirror = false;
break;
}
}

return pretest_ismirror;
}

/* check palindromes */
public boolean checkPalindromes(char stringArray[]) {
int i = 0 ;
int j = stringArray.length - 1;
boolean check_palindrone = true;
while(i <= j) {
if(stringArray[i] != stringArray[j]){
check_palindrone = false;
break;
}
++i;
--j;
}

return check_palindrone;
}

/* check mirrored */
public boolean checkMirroredString(String str) {

if(this.precheckMirroring(str.toCharArray()) == false) {
return false;
}

StringBuffer strbuf = new StringBuffer(str);

int inc = 0;

Map<String, String> map = new HashMap<String, String>();

map.put("A", "A");
map.put("E", "3");
map.put("H", "H");
map.put("I", "I");
map.put("J", "L");
map.put("L", "J");
map.put("M", "M");
map.put("O", "O");
map.put("T", "T");
map.put("U", "U");
map.put("V", "V");
map.put("W", "W");
map.put("X", "X");
map.put("Y", "Y");
map.put("Z", "5");
map.put("1", "1");
map.put("2", "S");
map.put("3", "E");
map.put("5", "Z");
map.put("8", "8");

int i = 0 ;
int j = strbuf.length() - 1;
while(i < j) {

String left = Character.toString(strbuf.charAt(i));
String right = Character.toString(strbuf.charAt(j));

// If the character is reverse to each other
if(map.get(left) != right || map.get(right) != left ){
return false;
}

if( left != right) {
return false;
}

++i;
--j;
}

return true;
}
}


在线法官给出了错误的答案。

最佳答案

大概

    isMirrored = new Main().precheckMirroring(stringArray);


应该

    isMirrored = new Main().checkMirroredString(stringArray);




另外-您不能使用 ==可靠地比较字符串,应该使用 equals



另外,在 checkMirroredString中,您可能不需要:

        if( left != right) {
return false;
}

关于java - ACM 401-在线回教在线法官给出错误的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30127127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com