我正在尝试做一些基于句子中字符数量的动态规划。英语字母表中哪个字母在屏幕上占像素最多?


当前回答

这也取决于字体。我在1或2年前用Processing和Helvetica做过这个,它是ILJTYFVCPAXUZKHSEDORGNBQMW,按增加像素的顺序。这个想法是用你正在看的字体在画布上绘制文本,计算像素,然后用HashMap或Dictionary排序。

当然,这可能与您的使用没有直接关系,因为它计算像素面积而不仅仅是宽度。可能也有点过头了。

void setup() { 
 size(30,30);
 HashMap hm = new HashMap();
 fill(255);
 PFont font = loadFont("Helvetica-20.vlw");
 textFont(font,20);
 textAlign(CENTER);

 for (int i=65; i<91; i++) {
    background(0);
    text(char(i),width/2,height-(textDescent()+textAscent())/2); 
    loadPixels();
    int white=0;
    for (int k=0; k<pixels.length; k++) {
       white+=red(pixels[k]);
    }
    hm.put(char(i),white);
  }

  HashMap sorted = getSortedMap(hm);

  String asciiString = new String();

  for (Iterator<Map.Entry> i = sorted.entrySet().iterator(); i.hasNext();) { 
    Map.Entry me = (Map.Entry)i.next();
    asciiString += me.getKey();
  }

  println(asciiString); //the string in ascending pixel order

}

public HashMap getSortedMap(HashMap hmap) {
  HashMap map = new LinkedHashMap();
  List mapKeys = new ArrayList(hmap.keySet());
  List mapValues = new ArrayList(hmap.values());

  TreeSet sortedSet = new TreeSet(mapValues);
  Object[] sortedArray = sortedSet.toArray();
  int size = sortedArray.length;

  // a) Ascending sort

  for (int i=0; i<size; i++) {
    map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])), sortedArray[i]);
  }
  return map;
}

其他回答

那么程序化的解决方案呢?

var capsIndex = 65; var smallIndex = 97 var div = document.createElement('div'); div.style.float = 'left'; document.body.appendChild(div); var highestWidth = 0; var elem; for(var i = capsIndex; i < capsIndex + 26; i++) { div.innerText = String.fromCharCode(i); var computedWidth = window.getComputedStyle(div, null).getPropertyValue("width"); if(highestWidth < parseFloat(computedWidth)) { highestWidth = parseFloat(computedWidth); elem = String.fromCharCode(i); } } for(var i = smallIndex; i < smallIndex + 26; i++) { div.innerText = String.fromCharCode(i); var computedWidth = window.getComputedStyle(div, null).getPropertyValue("width"); if(highestWidth < parseFloat(computedWidth)) { highestWidth = parseFloat(computedWidth); elem = String.fromCharCode(i); } } div.innerHTML = '<b>' + elem + '</b>' + ' won';

接下来是Ned Batchelder非常实用的回答,因为我来这里是想知道数字:

0000000000000000000000000000000000000000

1111111111111111111111111111111111111111

2222222222222222222222222222222222222222

3333333333333333333333333333333333333333

4444444444444444444444444444444444444444

5555555555555555555555555555555555555555

6666666666666666666666666666666666666666

7777777777777777777777777777777777777777

8888888888888888888888888888888888888888

9999999999999999999999999999999999999999

这段代码将获取数组中所有字符的宽度:

const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; var widths = []; var div = document.createElement('div'); div.style.float = 'left'; document.body.appendChild(div); var highestObservedWidth = 0; // widest characters (not just one) var answer = ''; for (var i = 0; i < alphabet.length; i++) { div.innerText = alphabet[i]; var computedWidthString = window.getComputedStyle(div, null).getPropertyValue("width"); var computedWidth = parseFloat(computedWidthString.slice(0, -2)); // console.log(typeof(computedWidth)); widths[i] = computedWidth; if(highestObservedWidth == computedWidth) { answer = answer + ', ' + div.innerText; } if(highestObservedWidth < computedWidth) { highestObservedWidth = computedWidth; answer = div.innerText; } } if (answer.length == 1) { div.innerHTML = ' Winner: ' + answer + '.'; } else { div.innerHTML = ' Winners: ' + answer + '.'; } div.innerHTML = div.innerHTML ; // console.log(widths); // console.log(widths.sort((a, b) => a - b));

Arial 30px在Chrome - W获胜。

这取决于字体。例如,交叉0所占的空间要比普通0大得多。

但如果要猜的话,我会选X或B。