计算字符的组合量并一一列举出来(java,算法探讨)

来源:互联网 发布:oa软件开发公司 编辑:程序博客网 时间:2024/05/17 03:35
保留版权,转载请注明出处!

今天在csdn逛的时候,碰到一个算法问题。

“写出用 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 
这些字符拼出来的所有长度小于等于12位的文件名(扩展名不用考虑)。”
引用自:http://topic.csdn.net/u/20080516/17/857098a2-aa24-4c98-9bd2-cc43676db3f4.html?seed=1061476272
这是我的实现,主要分了两个部分:
1、核心实现,以API方式只是写了两个static public的方法。
2、TestCase,这是单元测试,也是使用示范。
基本思想就是:
1、计算可能的组合量
2、for循环一一列出来,核心思想就是 数字的进制转换 。

下面是代码:
package niko7;


import java.math.BigInteger;

/**
 * 转载、引用必须保留此信息。
 * 
@author niko7, (顾法华,杭州)
 *
 
*/

public class MyTest
{
    
/**
     * 计算组合总量。
     * 
     * 总共有m个不同的字符,允许最多n个字符组成一字符串,计算总共有多少总组合。
     * 
@param charCount 字符的数量
     * 
@param maxLen 字符串的最大长度(最多允许的个数,1-maxLen都是符合要求的)
     * 
@return 可能的组合的总数
     
*/

    
public static BigInteger getTotal(int charCount,int maxLen)
    
{
        
if(1==charCount)
        
{
            
return BigInteger.valueOf(maxLen);
        }

        BigInteger m 
= BigInteger.valueOf(charCount);
        BigInteger total 
= m.pow(maxLen+1);
        total 
= total.subtract(BigInteger.ONE);
        total 
= total.divide(m.subtract(BigInteger.ONE));
        
return total.subtract(BigInteger.ONE);
    }


    
/**
     * 数字到字符串的转换方法。
     * 
     * 进制转换的修改版本,去掉了“零”的概念,允许高位为“0”,拼出各种字符组合。
     * 从外部输入 stringbuffer 和 radix ,是为了加快速度。
     * 
@param stringBuffer 转换后的结果放入这里。
     * 
@param num 需要转换的数字
     * 
@param radix 进制。注意和chars匹配!
     * 
@param chars 转换进制时使用的字符。注意和radix匹配!
     
*/

    
public static void number2String(StringBuffer stringBuffer,BigInteger num,BigInteger radix,char[] chars)
    
{
        BigInteger cIndex 
= num.remainder(radix);
        stringBuffer.insert(
0, chars[cIndex.intValue()]);
        
if (num.compareTo(radix) >= 0)
        
{
            num 
= num.subtract(cIndex);
            num 
= num.divide(radix);
            num 
= num.subtract(BigInteger.ONE);
            number2String(stringBuffer,num, radix, chars);
        }

    }

}

下面是TestCase:
package niko7;

import java.math.BigInteger;

import junit.framework.TestCase;

/**
 * 转载、引用必须保留此信息。
 * 
@author niko7, (顾法华,杭州)
 * email:niko7@163.com
 *
 
*/

public class MyTestTest extends TestCase
{

    
protected void setUp() throws Exception
    
{
        
super.setUp();
    }


    
protected void tearDown() throws Exception
    
{
        
super.tearDown();
    }

    
    
public void testGetTotal()
    
{
        
//就一个字符,最大12,那么就是12中可能
        assertEquals(BigInteger.valueOf(12),MyTest.getTotal(1,12));
        
//两个字符,最大3,那么就有14
        assertEquals(BigInteger.valueOf(14),MyTest.getTotal(2,3));
        
//3个字符,最大3,那么就有39种
        assertEquals(BigInteger.valueOf(39),MyTest.getTotal(3,3));
    }

    
    
public void testNumber2String()
    
{
        
char[] chars = "012".toCharArray();
        BigInteger charArrayLen 
= BigInteger.valueOf(chars.length);
        
        StringBuffer sb 
= new StringBuffer();
        BigInteger i 
= BigInteger.valueOf(38);
        String expected 
= "222";
        MyTest.number2String(sb,i,charArrayLen, chars);
        System.out.println(sb);
        assertEquals(expected, sb.toString());
        
        sb 
= new StringBuffer();
        i 
= BigInteger.valueOf(20);
        expected 
= "022";
        MyTest.number2String(sb,i,charArrayLen, chars);
        System.out.println(sb);
        assertEquals(expected, sb.toString());
    }


    
public void testDemo()
    
{
        
char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
        
//char[] chars = "0123456".toCharArray();
        
        
int charCount = chars.length;
        
int maxLen = 12;
        BigInteger charArrayLen 
= BigInteger.valueOf(charCount);
        
        BigInteger t 
= MyTest.getTotal(charCount,maxLen);
        System.out.println(
"共有可能的组合数量:"+t);
        
        
for(BigInteger i = BigInteger.ZERO; i.compareTo(t)<0; i=i.add(BigInteger.ONE))
        
{
            
if(i.compareTo(BigInteger.valueOf(30000))>0)
            
{
                System.out.println(
"运算量很大的,提前撤了!");
                
break;
            }

    
            StringBuffer sb 
= new StringBuffer();
            MyTest.number2String(sb,i,charArrayLen, chars);
            System.out.println(i
+""+sb);
        }

    }


}

 
原创粉丝点击