保留版权,转载必须注明出处!
本blog首发于CSDN,niko7的个人空间。2008年5月18日。
题目要求:
写出用 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
这些字符拼出来的所有长度小于等于12位的文件名(扩展名不用考虑)
直接针对题目进行了建模,代码没有前面那些方法简练。
使用了两个 static private inner class,没有使用递归。
建模、编写、调试、改进完成, 大概花了1个小时 。
本blog首发于CSDN,niko7的个人空间。2008年5月18日。
题目要求:
写出用 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
这些字符拼出来的所有长度小于等于12位的文件名(扩展名不用考虑)
直接针对题目进行了建模,代码没有前面那些方法简练。
使用了两个 static private inner class,没有使用递归。
建模、编写、调试、改进完成, 大概花了1个小时 。
package
niko7;
/**
* 直接针对题目要求建立了模型,然后模拟进位,实现字符组合枚举。
*
* 模型如下:
* 总体是模拟了 转轮式 计数器,我想大家在老式电表上都见过。
* Rnumber对象是一个轮子,RnumberList 是一串轮子构成的计数器。
*
* 转载、引用必须保留此段信息。
* @author niko7,(顾法华,杭州)
* @email niko7@163.com
*
*/
public class AdvDemo
{
/**
* 计数器上的一个轮子
* @author 顾法华,杭州
*
*/
static private class Rnumber
{
int index = -1;
char[] chars;
Rnumber high = null;
boolean goon = true;
/**
* 建立最左边的轮子时用这个构造函数,告诉它计数过程中使用的字符,并且隐含的告诉它进制是多少。
* @param chars
*/
public Rnumber(char[] chars)
{
this.chars = chars;
}
/**
* 除了最高位,其余的轮子都用这个构造函数,使其可以与它的相邻高位协同。
* @param high
*/
public Rnumber(Rnumber high)
{
this(high.chars);
this.high = high;
}
/**
* 加1操作。
*/
public void add()
{
this.index++;
if(this.index>=chars.length)//需要进位了
{
if(null!=this.high)
{
this.index = 0;
this.high.add();
}
else
{
this.index = -1;
this.goon = false;
}
}
}
/*
* 显示出从这个位置的左边几个轮子上的数字。
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuffer sb = new StringBuffer();
Rnumber p = this;
while(null!=p && p.index>=0)
{
sb.insert(0, p.chars[p.index]);
p = p.high;
};
return sb.toString();
}
}
/**
* 一串轮子构成的计数器。
* @author 顾法华,杭州
*
*/
static private class RnumberList
{
Rnumber high = null;
Rnumber low = null;
/**
* 告诉计数器进制过程中使用的字符,计数器需要的位数。隐含的告诉了所有位置上的进制。
* @param chars
* @param maxLen
*/
public RnumberList(char[] chars,int maxLen)
{
this.high = new Rnumber(chars);
Rnumber p = this.high;
for(int i=1; i<maxLen; i++)
{
this.low = new Rnumber(p);
p = this.low;
}
//将建好的计数器需要进入准备状态
this.add();
}
/**
* 计数器加1操作。
*/
public void add()
{
if(isFull())
{
throw new IllegalStateException("已经到顶了,不能再进位了!");
}
else
{
this.low.add();
}
}
/**
* 判断是否已经最大数值了。
* @return
*/
public boolean isFull()
{
return !this.high.goon;
}
/*
* 显示当前的数值,用构建时候给定的字符(包括字符的顺序)显示出来。
* @see java.lang.Object#toString()
*/
public String toString()
{
return (this.isFull()?"溢出!":this.low.toString());
}
}
public static void main(String[] args)
{
//允许组成的字符串的最大长度
int maxLen = 12;
//组成字符串的字符
char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
//char[] chars = "012".toCharArray();
RnumberList r = new RnumberList(chars,maxLen);
for( int i=0; !r.isFull() ; r.add())
{
System.out.println( i++ +" "+ r.toString() );
}
}
}
/**
* 直接针对题目要求建立了模型,然后模拟进位,实现字符组合枚举。
*
* 模型如下:
* 总体是模拟了 转轮式 计数器,我想大家在老式电表上都见过。
* Rnumber对象是一个轮子,RnumberList 是一串轮子构成的计数器。
*
* 转载、引用必须保留此段信息。
* @author niko7,(顾法华,杭州)
* @email niko7@163.com
*
*/
public class AdvDemo
{
/**
* 计数器上的一个轮子
* @author 顾法华,杭州
*
*/
static private class Rnumber
{
int index = -1;
char[] chars;
Rnumber high = null;
boolean goon = true;
/**
* 建立最左边的轮子时用这个构造函数,告诉它计数过程中使用的字符,并且隐含的告诉它进制是多少。
* @param chars
*/
public Rnumber(char[] chars)
{
this.chars = chars;
}
/**
* 除了最高位,其余的轮子都用这个构造函数,使其可以与它的相邻高位协同。
* @param high
*/
public Rnumber(Rnumber high)
{
this(high.chars);
this.high = high;
}
/**
* 加1操作。
*/
public void add()
{
this.index++;
if(this.index>=chars.length)//需要进位了
{
if(null!=this.high)
{
this.index = 0;
this.high.add();
}
else
{
this.index = -1;
this.goon = false;
}
}
}
/*
* 显示出从这个位置的左边几个轮子上的数字。
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuffer sb = new StringBuffer();
Rnumber p = this;
while(null!=p && p.index>=0)
{
sb.insert(0, p.chars[p.index]);
p = p.high;
};
return sb.toString();
}
}
/**
* 一串轮子构成的计数器。
* @author 顾法华,杭州
*
*/
static private class RnumberList
{
Rnumber high = null;
Rnumber low = null;
/**
* 告诉计数器进制过程中使用的字符,计数器需要的位数。隐含的告诉了所有位置上的进制。
* @param chars
* @param maxLen
*/
public RnumberList(char[] chars,int maxLen)
{
this.high = new Rnumber(chars);
Rnumber p = this.high;
for(int i=1; i<maxLen; i++)
{
this.low = new Rnumber(p);
p = this.low;
}
//将建好的计数器需要进入准备状态
this.add();
}
/**
* 计数器加1操作。
*/
public void add()
{
if(isFull())
{
throw new IllegalStateException("已经到顶了,不能再进位了!");
}
else
{
this.low.add();
}
}
/**
* 判断是否已经最大数值了。
* @return
*/
public boolean isFull()
{
return !this.high.goon;
}
/*
* 显示当前的数值,用构建时候给定的字符(包括字符的顺序)显示出来。
* @see java.lang.Object#toString()
*/
public String toString()
{
return (this.isFull()?"溢出!":this.low.toString());
}
}
public static void main(String[] args)
{
//允许组成的字符串的最大长度
int maxLen = 12;
//组成字符串的字符
char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
//char[] chars = "012".toCharArray();
RnumberList r = new RnumberList(chars,maxLen);
for( int i=0; !r.isFull() ; r.add())
{
System.out.println( i++ +" "+ r.toString() );
}
}
}