zoukankan      html  css  js  c++  java
  • 扑克牌排序

    图片弄这么大很难看,博客园没有「点击放大」的功能吗。。嫌麻烦,就不弄外链了。

    上周做了个华为的XX算法比赛,被虐出翔,第一道最简单的「扑克牌排序」就整了我到交卷都没做出来。搞得我一直怀疑自己是否不是这方面的料子。

    最近二师兄去Tencent实习了,不知道该往哪个方向搞啊!是不是要做出点改变啊。

    先把这道题解出来吧。代码在下面。

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Poker {
    
    	private static String pokerStr = "";
    	private static String[] pokerStrSplit;
    	private static ArrayList<Integer> higherThanTwo = new ArrayList<Integer>();
    	private static ArrayList<Integer> lowerThanTwo = new ArrayList<Integer>();
    	private static int j = 0;
    	private static int k = 0;
    	private static int l = 0;
    
    	public static void main(String args[]) {
    		Scanner cin = new Scanner(System.in);
    		System.out.println("input some card-values.");
    
    		pokerStr = cin.nextLine();
    		pokerStrSplit = pokerStr.split(",");
    
    		cin.close();
    		int num = pokerStrSplit.length;
    		int unsorted[] = new int[num];
    		int sorted[] = new int[num];
    		for (int i = 0; i < num; i++) {
    			unsorted[i] = Integer.parseInt(pokerStrSplit[i]);
    		}
    		for (int i = 0; i < num; i++) {
    			if (unsorted[i] > 2) {
    				higherThanTwo.add(unsorted[i]);
    				j++;
    			}// 大于2时降序排列
    			else if (unsorted[i] == 2 || unsorted[i] == 1) {
    				lowerThanTwo.add(unsorted[i]);
    				k = k + 1;
    			}
    		}
    		int[] higherThanTwoInt = new int[j];
    		int[] lowerThanTwoInt = new int[k];
    		for (int i = 0; i < j; i++)
    			higherThanTwoInt[i] = (Integer) higherThanTwo.get(i);
    		for (int i = 0; i < k; i++)
    			lowerThanTwoInt[i] = (Integer) lowerThanTwo.get(i);
    
    		int[] higherThanTwoSorted = insertionSort(higherThanTwoInt);
    		int[] lowerThanTwoSorted = insertionSort(lowerThanTwoInt);
    
    		for (int x = 0; x < lowerThanTwoSorted.length; x++)
    			sorted[x] = lowerThanTwoSorted[x];
    		for (int y = 0; y < higherThanTwoSorted.length; y++)
    			sorted[y + lowerThanTwoSorted.length] = higherThanTwoSorted[y];
    		StringBuffer sb = new StringBuffer();
    		for (int i : sorted) {
    			sb.append(i).append(",");
    		}
    		sb.deleteCharAt(sb.length() - 1);
    		System.out.println(sb);
    	}
    
    	static int[] insertionSort(int[] a) {
    		for (int p = 1; p < a.length; p++) {
    			int temp = a[p];
    			for (l = p; l > 0 && (temp - a[l - 1]) > 0; l--)
    				a[l] = a[l - 1];
    			a[l] = temp;
    		}
    		return a;
    	}
    }
    

      

    这算法写得。。一般人怎么可能在半小时想出来+写出来!我调试错误就用了好几小时。这是我的版本,应该有更好的。

    下面附上另外两道题:

  • 相关阅读:
    函数重载和函数指针在一起
    Uva
    Uva
    Uva
    Uva
    Uva
    CCPC-Wannafly-day5
    CCPC-Wannafly-day3
    CCPC-Wannafly-day2
    CCPC-Wannafly-Winter 2020.01.12总结
  • 原文地址:https://www.cnblogs.com/larrylawrence/p/3789601.html
Copyright © 2011-2022 走看看