此算法我在网上有搜索,暂时感觉我自己的算法最符合要求 :)
最近一个案子涉及到条码打印.要求是唯一的序列.中间有一个日期,最后3位为流水号.但打印的张数会超过999;
因此必须用到26个大写字母,现在要求如下.999以前就是普通数字(001~999),再多就开始在百位使用字母即999的后一个数字为A00此时十位和个位还不能使用字母(平时打印会少于3600张,为了客户看起来方便),
只有在百位将Z用完后才允许在十位使用字母,十位用完才允许个位使用字母
用36进制的话3位可以表示36*36*36=46656个数字
注意事项:
1)不能重复
2)数字个数应达到46656个
3)效率不能太低
4)请在本地测试没有重复序列号
因为家里的电脑没有安装
Visual Studio,所以格式就

.

private readonly char[] Char26
{}=new char
{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};

private readonly char[] Char36
{}=new char
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};

public string ConvertNum(int num)


{
if(num<0 || num>=46655)


{
throw new Exception("输入的数字超出范围");
}
else if(num>=12959)


{
string value="";
num-=12959;
char d=Char26[(int)(num/1296)];
num=num%1296;
char t=Char36[num%36];
char h=Char36[(int)(num/36)];
return value+h+t+d;
}
else if(num>3599)


{
string value="";
num-=3599;
char t=Char26[(int)(num/360)];
num=num%1296;
char h=Char36[num%36];
int d=num%10;
return value+h+t+d;
}
else if(num>=10)


{
string value="";
num-=3599;
char h=Char36[(int)(num/100)];
num=num%100;
int t=(int)(num/10);
int d=num%10;
return value+h+t+d;
}
else


{
return "00"+num;

}


}
由于在家没有代码,先给出测试的伪代码
//定义一个新list<string> nums
//i从0到46655循环
//输入i获取序列号num
//nums中是否包含num
//不包含就将num添加到nums
//包含就输出该重复序列
//循环完毕
//检查nums中的num个数是否为46656
有网友提到什么时候出现3字母.
我是在在Z99的后一个数字即为0A0,
而在0A9之后的一个数字为1A0
ZA9的后一个数字为0B0
ZZ9的后一个数字为00A
00Z的后一个数字为01A(请思考为什么)
我编写的算法已经用到案子中去了,且经过测试可以获得所有46656个数字.(代码在下周五放出,呵呵)
期间为了加快运算速度,我使用了表驱动法,取得了较好的计算效率.
另外计划写个关于表驱动法的小文