当前位置:编程学习 > JAVA >>

生成等宽字距字符串的算法

我们知道,在很多字体下面,每个英文字母的宽度是不一样的,就拿W和I来讲,W的宽度要大于I的宽度(字体的宽度),所以有时候我们会不太喜欢这样的定义。

 


以下我以Windows下默认的文件名称为例,我对比发现,等宽字符可以分成下面5组:

public static final char [] charsA = new char[]{'A','B','C','K','X'};
public static final char [] charsD = new char[]{'D','G','H','U'};
public static final char [] charsN = new char[]{'N','O','Q'};
public static final char [] charsF = new char[]{'F','L'};
public static final char [] charsE = new char[]{'E','P','S','T','Y','Z'};


每一组字符时等宽的,我们要想他们组合起来的字符串也是等宽的,那也有很多种情况,不过方法都是类似的,我以5个字母的字符串为例。

 


就是每个数组各取一个字符,例如各取第一个字符和各取第二个字符,分别是ADNFE和BGOLP,这两个字符串是等宽的。

 


下面给出Java语言版的算法,

 

 

[java]
public static final char [] charsA = new char[]{'A','B','C','K','X'}; 
public static final char [] charsD = new char[]{'D','G','H','U'}; 
public static final char [] charsN = new char[]{'N','O','Q'}; 
public static final char [] charsF = new char[]{'F','L'}; 
public static final char [] charsE = new char[]{'E','P','S','T','Y','Z'}; 
 
 
public static void main(String[] args) { 
 
    for(int i=0;i<100;i++){ 
        System.out.println(getRandomStr()); 
    } 

 
public static String getRandomStr(){ 
    StringBuffer strbuf = new StringBuffer(); 
     
    int orgIntArray[] = { 1, 2, 3, 4, 5 }; 
    int resultArray[] = zolltyRandom(orgIntArray); 
    int i = 0; 
    for(int j=0;j<resultArray.length;j++){ 
        switch(resultArray[j]){ 
            case 1:{ 
                i = (int) (Math.random()*5); 
                strbuf.append(charsA[i]); 
                break; 
            } 
            case 2:{ 
                i = (int) (Math.random()*4); 
                strbuf.append(charsD[i]); 
                break; 
            } 
            case 3:{ 
                i = (int) (Math.random()*3); 
                strbuf.append(charsN[i]); 
                break; 
            } 
            case 4:{ 
                i = (int) (Math.random()*2); 
                strbuf.append(charsF[i]); 
                break; 
            } 
            case 5:{ 
                i = (int) (Math.random()*6); 
                strbuf.append(charsE[i]); 
                break; 
            } 
        } 
    } 
     
    return strbuf.toString(); 

 
 
/**
 * 将原始数组重新随机排序(=洗牌)
 * 
 * @param orgIntArray
 *            例如{ 0, 1, 2, 3, 4, 5, 6, 7 }
 * @return 排列后的数组
 * @author zollty
 */ 
public static int[] zolltyRandom(int[] orgIntArray) { 
 
    Random rand = new Random(); 
 
    int pos, temp2; 
 
    int returnValue[] = new int[orgIntArray.length]; 
 
    for (int i = 0, r = orgIntArray.length; i < orgIntArray.length - 1; i++, r--) { 
        pos = Math.abs(rand.nextInt()) % r; 
 
        returnValue[i] = orgIntArray[pos]; 
 
        // [pos]已使用,与最后那个未使用的交换  
        temp2 = orgIntArray[pos]; 
        orgIntArray[pos] = orgIntArray[r - 1]; 
        orgIntArray[r - 1] = temp2; 
    } 
 
    returnValue[orgIntArray.length - 1] = orgIntArray[0]; 
 
    return returnValue; 

 public static final char [] charsA = new char[]{'A','B','C','K','X'};
 public static final char [] charsD = new char[]{'D','G','H','U'};
 public static final char [] charsN = new char[]{'N','O','Q'};
 public static final char [] charsF = new char[]{'F','L'};
 public static final char [] charsE = new char[]{'E','P','S','T','Y','Z'};
 
 
 public static void main(String[] args) {

  for(int i=0;i<100;i++){
   System.out.println(getRandomStr());
  }
 }
 
 public static String getRandomStr(){
  StringBuffer strbuf = new StringBuffer();
  
  int orgIntArray[] = { 1, 2, 3, 4, 5 };
  int resultArray[] = zolltyRandom(orgIntArray);
  int i = 0;
  for(in

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,