六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 22|回复: 0

有n张扑克牌,每张牌的取值范围是:2,3,4,5,6,7,8,9,

[复制链接]

升级  72.67%

126

主题

126

主题

126

主题

举人

Rank: 3Rank: 3

积分
418
 楼主| 发表于 2013-2-4 19:56:10 | 显示全部楼层 |阅读模式
import java.util.Arrays;


/**
* (c)2010 华润(集团)有限公司版权所有. 保留所有权利.
*
* 文件名称:Ha.java
* 程序说明:
* 创建日期:Jun 18, 2010
* @version 1.0
*/

/**
* @类名:Ha
* @描述:TODO
* @创建日期:Jun 18, 2010 4:43:36 PM
* @修改记录:
*
* @author WangYanFeng
*/

public class Ha {

    /*
     * 有n张扑克牌,每张牌的取值范围是:2,3,4,5,6,7,8,9,
     * 10,J,Q,K,A。在这n张牌中找出顺子(5张及5张以上的连续
     * 的牌),并将这些顺子打印出来。
     */

//    public static void main(String[] args) {
//     // TODO Auto-generated method stub
//     String str = "2,3,4,5,6,7,8,9,10,J,Q,K,A";
//     wantA(str,5);
//     }
//      
//     /**
//     *  
//     * @param str  
//     * @param limit 最少几个连续
//     */
//     public static void wantA(String str,int limit){
//     String[] array = str.split(",");
//     for(int i = 0;i<array.length - limit;i++){
//     for(int j = i+limit;j<array.length - 1;j++){
//     for(int k = i;k<j;k++){
//     System.out.print(array[k]);
//     }
//     System.out.println();
//     }
//     }
//     }
//    }
   
//    public static void main(String[] args) {
//        String str = "2,3,4,5,6,7,8,9,10,J,Q,K,A";
//        wantA(str,5);
//      }
//      
//      public static void wantA(String str,int limit){
//        String[] array = str.split(",");
//        for(int k = 0; k < array.length - limit + 1; k ++){
//            for(int j = limit + k ; j < array.length + 1;j ++){
//                for(int i = k; i < j; i ++){
//                    System.out.print(array[i]);
//                }
//                System.out.println();
//            }
//        }
//      }

    public static void main(String[] args) {
        char[] cs = { 'Z', 'C', 'D', 'E', '1', '2', '3', '4', '8', '5', '6',
                'A', '7', 'B' };
        Arrays.sort(cs);
        System.out.println(Arrays.toString(cs));
        // 连续的字符长度默认设置为5
        int length = 5;
        while (length < cs.length) {
            for (int i = 0; i < cs.length - length; i++) {
                char[] temp = getIncreaseChars(new String(cs).substring(i,
                        i + length).toCharArray(), length);
                if (temp != null)
                    System.out.println(Arrays.toString(temp));
            }
            length++;
        }
    }

    // 专门比较连续的n个字符或数字
    public static char[] getIncreaseChars(char[] cs, int n) {
        if (cs.length != n)
            throw new RuntimeException("char array's length must be " + n);
        char[] csCopy = cs.clone();
        Arrays.sort(csCopy);
        for (int i = 0; i < n - 1; i++) {
            if (csCopy[i + 1] != csCopy[i] + 1)
                return null;
        }
        return csCopy;
    }

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表