给出一串随机顺序的数字,你必须以十进制格式以严格降序打印它们 [英] A string of numbers in random order is given and you have to print them in decimal format in strictly decreasing order

查看:20
本文介绍了给出一串随机顺序的数字,你必须以十进制格式以严格降序打印它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:nieignhtesevfouenr答:9874

Example: nieignhtesevfouenr ans: 9874

请有人回答这个问题.谢谢.

Someone please answer this question. Thank You.

推荐答案

你可以应用这个算法:

  • 首先统计输入字符串中每个字母出现的次数.例如,示例输入字符串出现以下情况:

  • First count the occurrence of each letter in the input string. For example, the example input string has these occurrences:

{
  "e": 4,
  "f": 1,
  "g": 1,
  "h": 1,
  "i": 2,
  "n": 3,
  "o": 1,
  "r": 1,
  "s": 1,
  "t": 1,
  "u": 1,
  "v": 1,
  "w": 0,
  "x": 0,
  "z": 0
}

我还包括了未使用的字母,因为它们会在二"、六"中起作用.和零",但在此示例输入中未表示它们.

I also included unused letters, as they would play a role in "two", "six" and "zero", but they are not represented in this example input.

现在我们可以观察到:

  • 每一个z"在输入中属于零";
  • 每一个w"属于二";
  • 每一个你"属于四";
  • 每一个x"属于六";
  • 每一个g"属于八"

如果你找到z"的数量在输入字符串中,您可以推断出很多出现的零",并且您可以从z"、e"、r"和o"中扣除该计数.那么多0"应记录输出.

If you find the number of "z" in the input string, you can deduct there are that many occurrences of "zero", and you can deduct that count from "z", "e", "r", and "o". That many "0" should be recorded for the output.

完成上述操作后,我们可以继续执行以下操作:

Once we have done that for the above, we can continue with the following:

  • 每个剩余o"属于一个一个"(因为此时已经考虑了所有零"、二"、四").
  • 每个剩余h"属于三"
  • 每一个剩余的f"属于五"
  • 每一个剩余的s"属于七"
  • Every remaining "o" belongs to a "one" (since all "zero", "two", "four" were already accounted for by this time).
  • Every remaining "h" belongs to a "three"
  • Every remaining "f" belongs to a "five"
  • Every remaining "s" belongs to a "seven"

在考虑到这些之后,我们最终可以得出结论:

And after those have been accounted for, we can finally conclude that:

  • 每一个剩余的i"属于九"

我们可以稍微简化一下,因为有些字母在决定代表哪些数字方面并没有真正发挥作用.例如,在上面的逻辑中,我们不关心n"的数量.或e",所以当我们检测到一个"等时,我们最好不要减少它们的数量.上面列出了我们唯一关心的 10 个字母.

We can simplify a bit, because some letters don't really play a role in deciding which digits are represented. For instance, in the above logic, we don't care about the number of "n" or "e", so we might as well not reduce their count when we detect a "one", ...etc. The only 10 letters we care about are listed above.

最后,我们使用每个数字的计数来构建排序字符串.

Finally we use the counts for each digit to build the sorted string.

这是一个 JavaScript 实现.它运行示例输入的算法.没有输入验证...假设是数字名称的有效组合:

Here is an implementation in JavaScript. It runs the algorithm for the example input. There is no input validation... It is assumed to be a valid combination of digit names:

// reference data. The identifying letter is put at the front, 
//    and the letters that still matter for another digit
//    follow it.
let map = [
    ["zo", "0"],
    ["wo", "2"],
    ["ufo", "4"],
    ["xis", "6"],
    ["gih", "8"],
    ["o", "1"],
    ["h", "3"],
    ["fi", "5"],
    ["s", "7"],
    ["i", "9"]
];

// The algorithm
function decimals(s) {
    // Count occurrences of letters
    let charCount = {};
    for (let c of "efghinorstuvwxz") charCount[c] = 0;
    for (let c of s) charCount[c]++;
    // Recognise digits by their identifying letter
    let digitCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    for (let [name, digit] of map) {
        let frequency = charCount[name[0]];
        for (let letter of name) charCount[letter] -= frequency;
        digitCount[digit] = frequency;
    }
    // Build the result string
    let result = "";
    for (let digit of "9876543210") result += digit.repeat(digitCount[digit]);
    return result;
}

// Example run
let result = decimals("nieignhtesevfouenr");
console.log(result);

这篇关于给出一串随机顺序的数字,你必须以十进制格式以严格降序打印它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆