杭电 ACM 1106 JAVA实现

package testa;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner point = new Scanner(System.in);
        while (point.hasNextLine()) {
            String numStr = point.nextLine();
            char[] outputChar = replaceZero(numStr).toCharArray();
            int[] tempList = new int[outputChar.length];
            // 处理中间结果
            int j = solveMediumResult(outputChar, tempList);
            int[] list = new int[j + 1];
            for (int item = 0; item <= j; item++) list[item] = tempList[item];
            // 快速排序
            fastSorted(list, 0, list.length - 1);
            for (int k = 0; k < list.length - 1; k++) System.out.print(list[k] + " ");
            System.out.println(list[list.length - 1]);
        }
    }
    
    /**
     * 替换0
     * 
     * @param inputStr
     * @return
     */
    public static String replaceZero(String inputStr) {
        char[] charList = inputStr.toCharArray();
        inputStr = "";
        for (int i = 0; i < charList.length; i = i + 1) {
            if (charList[i] == '5') {
                if (i > 0 && charList[i - 1] == '5') {
                    continue;
                }
                inputStr = inputStr + " ";
            } else if (i > 0 && charList[i] == '0' && charList[i - 1] == '5') {
                while (i < charList.length - 1 && charList[i + 1] == '0') { i++;}
                inputStr = inputStr + charList[i];
            } else if (i == 0 && charList[i] == '0') {
                while (i < charList.length - 1 && charList[i + 1] == '0') { i++;}
                inputStr = inputStr + charList[i];
            } else {
                inputStr = inputStr + charList[i];
            }
        }
        return inputStr;
    }

    /**
     * 快速排序
     * 
     * @param list
     */
    public static void fastSorted(int[] list, int i, int j) {
        if (i >= j) {
            return;
        }
        int needToSortLen = j;
        int referIndex = i;
        while (i != j) {
            while (list[referIndex] <= list[j] && j > i) { j--;}
            while (list[referIndex] >= list[i] && i < j) { i++;}
            int temp = list[j];
            list[j] = list[i];
            list[i] = temp;
        }
        int tempReferValue = list[referIndex];
        list[referIndex] = list[i];
        list[i] = tempReferValue;
        fastSorted(list, referIndex, i - 1);
        fastSorted(list, i + 1, needToSortLen);
    }

    /**
     * 
    * @Name: solveMediumResult 
    * @Description: 处理中间结果
    * @param @param outputChar
    * @param @param tempList
    * @param @return     
    * @return int    
    * @throws
     */
    private static int solveMediumResult(char[] outputChar, int[] tempList) {
        String tempStr = "";
        int j = 0;
        // convert char to int
        for (int i = 0; i < outputChar.length; i++) {
            if (outputChar[i] == ' ') {
                if (i == outputChar.length - 1) {
                    break;
                }
                if (!tempStr.equals("")) {
                    tempList[j] = Integer.parseInt(tempStr);
                    j++;
                    tempStr = "";
                }
                continue;
            }
            tempStr = tempStr + outputChar[i];
        }
        tempList[j] = Integer.parseInt(tempStr);
        return j;
    }
}

 

转载于:https://my.oschina.net/xiaomianyang/blog/733664

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值