演算法筆記_153:演算法提高 判斷名次(Java)

來源:互聯網
上載者:User

標籤:substring   scan   har   一句話   int   src   add   輸入   bst   

目錄

1 問題描述

2 解決方案

  1 問題描述問題描述

 

  某場比賽過後,你想要知道A~E五個人的排名是什麼,於是要求他們每個人說了一句話。(經典的開頭……-_-!)得了第1名的人23,說了假話;得了第5名的人不好意思,也說了假話;為了使求解問題簡單,第3名同樣說了假話。(奇數名次說假話)

 

輸入格式

 

  共5行,各行依次表示A~E說的話。
  每行包含一個形如“A>=3”的名次判斷,即一個大寫字母+關係運算子+一個數字,不包含空格。
  大寫字母A~E,關係運算<、<=、=、>=、>、!=,數字1~5。注意:等於是“=”不是“==”!

 

輸出格式

 

  可能有多解,請按照字典序輸出排名序列,每個解一行
  最後一行輸出解的數量

 

範例輸入

 

A=2
D=5
E>3
A>2
B!=1

 

範例輸出

 

ACDEB
AECBD
BADCE
BCADE
BDACE
CEADB
CEBDA

 

 

2 解決方案

具體代碼如下:

import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;public class Main {    public static String[] say = new String[5];    public static ArrayList<String> temp = new ArrayList<String>();    public static ArrayList<String> result = new ArrayList<String>();        public void swap(char[] A, int i, int j) {        char s = A[i];        A[i] = A[j];        A[j] = s;    }        public void dfs(char[] A, int step) {        if(step == A.length) {            StringBuilder s = new StringBuilder("");            for(int i = 0;i < A.length;i++)                s.append(A[i]);            temp.add(s.toString());        } else {            for(int i = step;i < A.length;i++) {                swap(A, i, step);                dfs(A, step + 1);                swap(A, i, step);            }        }    }        public boolean judge(int a, String o, int p, int b) {        if(b == 1) {  //此次說話為真            if(o.equals(">")) {                if(a > p)                    return true;                else                    return false;            } else if(o.equals(">=")) {                if(a >= p)                    return true;                else                    return false;            } else if(o.equals("=")) {                if(a == p)                    return true;                else                    return false;            } else if(o.equals("!=")) {                if(a != p)                    return true;                else                    return false;            } else if(o.equals("<")) {                if(a < p)                    return true;                else                    return false;            } else if(o.equals("<=")) {                if(a <= p)                    return true;                else                    return false;            }        } else if(b == 0) {  //此次說話為假            if(o.equals(">")) {                if(a <= p)                    return true;                else                    return false;            } else if(o.equals(">=")) {                if(a < p)                    return true;                else                    return false;            } else if(o.equals("=")) {                if(a != p)                    return true;                else                    return false;            } else if(o.equals("!=")) {                if(a == p)                    return true;                else                    return false;            } else if(o.equals("<")) {                if(a >= p)                    return true;                else                    return false;            } else if(o.equals("<=")) {                if(a > p)                    return true;                else                    return false;            }        }        return false;    }        public void getResult(int i, int j) {        for(int t = 0;t < temp.size();t++) {            String s = temp.get(t);            boolean judge1 = true;            for(int m = 0;m < 5;m++) {                char a = say[m].charAt(0);                String o = "";                int p = say[m].charAt(say[m].length() - 1) - ‘0‘;                if(say[m].length() == 3) {                    o = o + say[m].substring(1, 2);                } else {                    o = o + say[m].substring(1, 3);                }                if(i == m || j == m) {                    judge1 = judge(s.indexOf(a) + 1, o, p, 1);                } else {                    judge1 = judge(s.indexOf(a) + 1, o, p, 0);                }                if(judge1 == false)                    break;            }            if(judge1 == false)                continue;            int a1 = s.indexOf((‘A‘+i));            int a2 = s.indexOf((‘A‘+j));            if((a1 == 1 && a2 == 3) || (a1 == 3 && a2 == 1)) {                judge1 = true;            } else {                judge1 = false;            }                      if(judge1 == true) {                if(!result.contains(s))                    result.add(s);            }        }    }        public static void main(String[] args) {        Main test = new Main();        String A = "ABCDE";        char[] B = A.toCharArray();        test.dfs(B, 0);        Scanner in = new Scanner(System.in);        for(int i = 0;i < 5;i++)            say[i] = in.next();        for(int i = 0;i < 5;i++) {            for(int j = i + 1;j < 5;j++)                test.getResult(i, j);        }        Collections.sort(result);        for(int i = 0;i < result.size();i++)            System.out.println(result.get(i));        System.out.println(result.size());    }}

 

演算法筆記_153:演算法提高 判斷名次(Java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.