华为2014 第三题地铁换乘 java实现

   这一题,应该是坐出来了吧,自己小小测试了一下,貌似是没有问题的。我采取的方法是将T1、T2转换为A或B里面的其中一站,所以这就需要将输入的站数进行转换,考虑的情况挺多的吧,我感觉这方法挺蠢的,是属于做数学题方式做出来的,在算法实在没有什么可取之处,贴出来只是说自己想了老久,就当给自己的小小奖励吧~~o(>_<)o ~~

 

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Path {

    private static char h1, h2;
    private static int n1, n2;
    private static String s1, s2;

    public static void main(String[] args) {

        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        int flag,p=0;

        while (cin.hasNext()) {

            s1 = cin.next();
            s2 = cin.next();
            
            flag=Flag();
            
            
            
            if(flag==1){
                big();
                
                if((n2-n1)>=10){
                    p=(19-n2)+n1+1;
                }else{
                    p=n2-n1+1;
                }
            }
            
            if(flag==2){
                
                big();
                
                p=n2-n1+1;
            }
            
            if(flag==3){
                
                p=add(n1, n2);
                
            
            }
            
            if(flag==4){
                p=add(n2,n1);
                
            }
            
            System.out.println("您至少需要坐"+p+"站");

        }

    }

    public static int Flag() {

        h1 = s1.charAt(0);
        h2 = s2.charAt(0);
        n1 = Integer.parseInt(s1.substring(1, s1.length()));
        n2 = Integer.parseInt(s2.substring(1, s2.length()));

        if (h1 == 'A' && h2 == 'A') {
            n1 = A(n1);
            n2 = A(n2);
            return 1;
        }

        if (h1 == 'B' && h2 == 'B') {

            n1 = B(n1);
            n2 = B(n2);

            return 2;
        }

        if (h1 == 'T') {        
            if (h2 == 'T') {
                n1=T(n1,1);
                n2=T(n2, 1);
                return 1;
            }
            if (h2 == 'A') {
                n1=T(n1,1);
                n2=A(n2);
                return 1;
            }
            
            if(h2=='B'){
                n1=T(n1,2);
                n2=B(n2);
                return 2;
            }
        }
        
        if(h2=='T'){
            
            if (h1 == 'A') {
                n2=T(n2,1);
                n1=A(n1);
                return 1;
            }
            
            if(h1=='B'){
                n2=T(n2,2);
                n1=B(n1);
                return 2;
            }
            
        }
        
        if(h1=='A'){
            n1=A(n1);
            n2=B(n2);
            
            return 3;
        }else{
            
            n1=B(n1);
            n2=A(n2);
            
            return 4;
            
        }

    }

    public static int A(int n) {

        if (n > 9) {
            if (n >= 10 && n <= 13) {
                n++;
            } else {
                n = n + 2;
                if (n == 20)
                    n = 1;
            }
        }

        return n;

    }

    public static int B(int n) {

        if (n > 5) {
            if (n >= 6&&n <= 10) {
                n++;

            } else {
                n = n + 2;
            }
        }

        return n;

    }

    public static int T(int n, int i) {

        if (i == 1) {

            if (n == 1) {
                n = 10;
            } else {
                n = 15;
            }
        } else {
            if (n == 1) {
                n = 6;
            } else {
                n = 12;
            }
        }

        return n;

    }
    
    public static void big(){
        
        
        if(n1>n2){            
            n1=n1+n2;
            n2=n1-n2;
            n1=n1-n2;            
        }
        
    }
    
    public static int add(int a,int b){
        List<Integer> list = new ArrayList<Integer>();
        int p1,p2,p3,p4;
        
        p1=p(a,1);
        p2=p(a,2);
        p3=Math.abs(b-6);
        p4=Math.abs(b-12);
        
        list.add(p1+p3+5);
        list.add(p1+p4);
        list.add(p2+p3);
        list.add(p2+p4+5);
        
        Collections.sort(list);
        
        return list.get(0);
    }
    
    public static int p(int a,int i){
        
        int k1,k2;
        
        if(i==1){
            k1=Math.abs(a-15)+1;
            k2=5+a;
            return (k1<k2)?k1:k2;
        }else{
            k1=Math.abs(a-10)+1;
            k2=10+a;
            return (k1<k2)?k1:k2;
        }
        
    }

}
View Code

 

posted @ 2013-10-03 09:42  逢尘化雪  阅读(592)  评论(0编辑  收藏  举报