计算字符的组合量并一一列举出来(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循环一一列出来,核心思想就是 数字的进制转换 。
下面是代码:
下面是TestCase:
今天在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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
- 计算字符的组合量并一一列举出来(java,算法探讨)
- 还是字符组合问题,直接针对题目建模(java,算法探讨)
- 计算组合的算法
- 一个字符串有两个A,计算里面的字符个数,并打印出来
- java算法--字符组合排序
- 计算组合数的算法
- [python]My Unique JsonDiff算法——如何计算2个json串之间的差距并Diff出来(一):编辑距离(Levenshtein)算法
- java算法之字符组合排序
- 计算java 代码量的小工具
- sku组合查询算法探讨
- Java语言的字符编码问题探讨
- 字符组合问题的分治算法
- 【算法题】字符的所有组合
- 用改过的“数字进制转换”枚举字符组合,这是简化的版本。(java,算法研究)
- (java)手机键盘上字符的组合方式
- js计算出来的文件md5值跟java计算出来的不一致
- 用JAVA编写一个算法实现对一个字符数组的所有元素的所有组合
- 关于edit控件自动换行算法,计算量的问题
- 威迅教育软件工程师毕业了
- ffu噪音
- 焦点滚动图片
- 今天见到了袁鸣和当当网的俞渝
- 采用p6spy完整显示hibernate的SQL语句
- 计算字符的组合量并一一列举出来(java,算法探讨)
- 设置网页背景图片平铺方式 background-repeat 属性
- 求解KMP算法及next值
- WEB程序员学习JS快速指南--基础一
- SRC的RPM包安装方法
- AdvancedArray2.java
- C /C 笔试题-C/C 笔试、面试题目大汇总
- 两首才流传开的2008童谣,看了鼻子酸酸,请珍惜生活_赵老猫—环保居士的BLOG_新浪博客
- 关于C++中的类型转换