加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 用于查找数字总和等于数字显示中的段数的时间的聪明算法

发布时间:2020-12-15 19:34:01 所属栏目:百科 来源:网络整理
导读:所以,我的朋友今天早上给我发了一个谜题: Find the number of distinct times of the day (using 24-hour display and assuming that morning hours are presented as 8:15 as opposed to 08:15) where the number of segments are equal to the sum of the
所以,我的朋友今天早上给我发了一个谜题:

Find the number of distinct times of
the day (using 24-hour display and
assuming that morning hours are
presented as 8:15 as opposed to 08:15)
where the number of segments are equal
to the sum of the digits. Eg. 8:15 has
7+2+5=14 segments in electronic format
and the sum of the digits is 8+1+5=14,
so it qualifies as a case.

所以我在C#3.0中提出了以下简单(但是迟钝的暴力)解决方案:

// Number of segments in each digit
var digitMap = 
    new Dictionary<char,int>
    {
        {'0',6},{'1',2},{'2',5},{'3',{'4',4},{'5',{'6',{'7',3},{'8',7},{'9',5}
    };

var numMatches = (
            from h in Enumerable.Range(0,24)
            from m in Enumerable.Range(0,60)
            select h.ToString() + m.ToString().PadLeft(2,'0') into t 
            let chars = t.ToCharArray()
            where chars.Sum(c => int.Parse(c.ToString())) == chars.Sum(c => digitMap[c])
            select t).Count();

但是,他补充说:

Brute force approach is not allowed.

我已经考虑了一段时间了,我正在努力想出一个更聪明的算法.我正在沿着预先过滤不可能性的路线(例如,数字总和小于6的时间,因为那是最小的段总和) – 但最后我想这只会导致更小的解空间,然后是暴力强迫.

无论如何,我认为把它扔出去是一个有趣的问题,看看是否有人能想出一个更聪明的方法.

解决方法

每个数字将始终具有相同的偏移量. 8总是让你的片段低一个,1总是让你的片段更高,5总是相同,等等.一旦你知道了这个值,你就可以很快地生成最终结合你的有效组合平等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读