블 루 브리지 컵알고리즘 증가순 위 를 판단 하 다
14161 단어 알고리즘
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/** * @author * */
public class Main {
private static String[] strArr=new String[5];
private static ArrayList<String> list=new ArrayList<String>();
/** * @param args */
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++){
strArr[i]=sc.next();
}
allSort(0,5,new char[]{'A','B','C','D','E'});
Collections.sort(list);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println(list.size());
}
private static void allSort(int start,int end,char[] arr){
if(start==end){
String s=new String(arr);
if(judge(s)){
list.add(s);
}
}else{
for(int i=start;i<end;i++){
char temp=arr[start];
arr[start]=arr[i];
arr[i]=temp;
allSort(start+1,end,arr);
temp=arr[start];
arr[start]=arr[i];
arr[i]=temp;
}
}
}
private static boolean judge(String s){
char num1=s.charAt(0);
char num3=s.charAt(2);
char num5=s.charAt(4);
String[] temp=new String[5];
for(int i=0;i<5;i++){
temp[i]=strArr[i].substring(0);
}
reverse(num1,temp);
reverse(num3,temp);
reverse(num5,temp);
if(check(s,temp)){
return true;
}else{
return false;
}
}
private static boolean check(String s, String[] temp) {
for(int i=0;i<5;i++){
String talk=temp[i];
if(talk.length()==3){
if(talk.charAt(1)=='<'){
int realNumIndex=s.indexOf(""+talk.charAt(0))+1;//num
int assumeNumIndex=talk.charAt(talk.length()-1)-'0';//num
if(!(realNumIndex<assumeNumIndex)){
return false;
}
}else if(talk.charAt(1)=='='){
int realNumIndex=s.indexOf(""+talk.charAt(0))+1;//num
int assumeNumIndex=talk.charAt(talk.length()-1)-'0';//num
if(!(realNumIndex==assumeNumIndex)){
return false;
}
}else{
int realNumIndex=s.indexOf(""+talk.charAt(0))+1;//num
int assumeNumIndex=talk.charAt(talk.length()-1)-'0';//num
if(!(realNumIndex>assumeNumIndex)){
return false;
}
}
}else{
if(talk.charAt(1)=='>'){
int realNumIndex=s.indexOf(""+talk.charAt(0))+1;//num
int assumeNumIndex=talk.charAt(talk.length()-1)-'0';//num
if(!(realNumIndex>=assumeNumIndex)){
return false;
}
}else if(talk.charAt(1)=='!'){
int realNumIndex=s.indexOf(""+talk.charAt(0))+1;//num
int assumeNumIndex=talk.charAt(talk.length()-1)-'0';//num
if(!(realNumIndex!=assumeNumIndex)){
return false;
}
}else{
int realNumIndex=s.indexOf(""+talk.charAt(0))+1;//num
int assumeNumIndex=talk.charAt(talk.length()-1)-'0';//num
if(!(realNumIndex<=assumeNumIndex)){
return false;
}
}
}
}
return true;
}
private static void reverse(char num,String[] temp){//num
switch(num){
case 'A':temp[0]=change(temp[0]);break;
case 'B':temp[1]=change(temp[1]);break;
case 'C':temp[2]=change(temp[2]);break;
case 'D':temp[3]=change(temp[3]);break;
case 'E':temp[4]=change(temp[4]);break;
}
}
//<、<=、=、>=、>、!=
private static String change(String s){
if(s.length()==3){
if(s.charAt(1)=='<'){
s=s.replace("<",">=");
}else if(s.charAt(1)=='='){
s=s.replace("=","!=");
}else{
s=s.replace(">","<=");
}
}else{
if(s.charAt(1)=='>'){
s=s.replace(">=","<");
}else if(s.charAt(1)=='!'){
s=s.replace("!=","=");
}else{
s=s.replace("<=",">");
}
}
return s;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Codility Lesson3】FrogJmpA small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.