[Answer] Mathematics × Programming Competition #8 [答案] 數學 × 程式編寫比賽 (第八回)steemCreated with Sketch.

in #contest7 years ago (edited)

@kenchung 's Contest is fun, as always, thank you!

This puzzle is simple to understand.

If you flip this over 180 degree, you will get

So how many numbers do you need to represent from 0000 to 9999?

First, we need a function to tell if a digit can be flipped... if not, e.g. like 7, we need to return false.

function getRevDig(x) {
    switch (x) {
        case 0: return 0;
        case 1: return 1;
        case 2: return 2;
        case 5: return 5;
        case 6: return 9;
        case 8: return 8;
        case 9: return 6;
        default: return false;
    }
}

Now, given a four digit number, we need to return its flipped version, to get each digit, you can use module operator and division.

function getRev(x) {
    var a = x % 10;
    a = getRevDig(a);
    if (a === false) return false;
    var b = Math.floor(x / 10) % 10;
    b = getRevDig(b);
    if (b === false) return false;
    var c = Math.floor(x / 100) % 10;
    c = getRevDig(c);
    if (c === false) return false;  
    var d = Math.floor(x / 1000);
    d = getRevDig(d);
    if (d === false) return false;  
    return (a * 1000 + b * 100 + c * 10 + d);
}

The rest is straightforward, just to check and mark the number and its rotated version in a dictionary-like data structure (or array).


var total = 0;
var a = {};
for (var i = 0; i <= 9999; ++i) {
    if (!(i in a)) {
        total ++;
        a[i] = 1;
        var x = getRev(i);
        if (x !== false) {
            a[x] = 1;
        }
    }
}
console.log(total);

That should give you the correct answer.


Designed by @nicolemoker


@kenchung数学及编程比赛 真好玩,可以练练脑,练练手,防止老年痴呆。

这次比赛不难,就是把一个4个数字的电子屏倒过来,

就成这样:

问题是我们需要多少块(每块倒着可以用)

首先,写一个函数,用于返回一个数字是否可以倒过来,像7这种倒过来没有意义的就可以返回FALSE区分。

function getRevDig(x) {
    switch (x) {
        case 0: return 0;
        case 1: return 1;
        case 2: return 2;
        case 5: return 5;
        case 6: return 9;
        case 8: return 8;
        case 9: return 6;
        default: return false;
    }
}

给定一个4位数,计算倒过来的数字,如果倒过来无意义,返回FALSE。

function getRev(x) {
    var a = x % 10;
    a = getRevDig(a);
    if (a === false) return false;
    var b = Math.floor(x / 10) % 10;
    b = getRevDig(b);
    if (b === false) return false;
    var c = Math.floor(x / 100) % 10;
    c = getRevDig(c);
    if (c === false) return false;  
    var d = Math.floor(x / 1000);
    d = getRevDig(d);
    if (d === false) return false;  
    return (a * 1000 + b * 100 + c * 10 + d);
}

然后就是从0到9999,每次检查是否已经出现了, 否则统计加1,然后把倒过来的数字也标记上了。


var total = 0;
var a = {};
for (var i = 0; i <= 9999; ++i) {
    if (!(i in a)) {
        total ++;
        a[i] = 1;
        var x = getRev(i);
        if (x !== false) {
            a[x] = 1;
        }
    }
}
console.log(total);

这样就能打印出正确的答案了,希望能获奖!


@justyyhttps://justyy.com 的博主,在 @tumutanzi 大哥 的介绍下加入 STEEMIT,写些帖子挣些小钱养家糊口。


@justyy 也是CN 区的点赞机器人,对优质内容点赞,只要代理给 @justyy 每天收利息(年化率10%)并能获得一次至少2倍(VP 200%+)的点赞,大鱼 @htliao 都加入了这个计划(530 SP)表示支持。

  1. cn区最低保障系统 上线了!
  2. cn区低保计划(鼓励新人)真的适合你么?
  3. 今天(2017-10-20) 银行股东名单

Sort:  

Nicely done and a great explanation. Just thinking 2 can be flipped or not?

2 flipped is 2.

还没到时间呢

差不多啦,所以没有把答案放出来……

也对哈,你这没有答案,我也写去。

Calling @originalworks :)
img credz: pixabay.com
Nice, you got a 50.0% @trafalgar upgoat, thanks to @justyy
Want a boost? Minnowbooster's got your back!

The @OriginalWorks bot has determined this post by @justyy to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
For more information, Click Here!
Special thanks to @reggaemuffin for being a supporter! Vote him as a witness to help make Steemit a better place!

看來每次靠你們寫題解已經可以了 哈哈

哈哈,希望能获奖!

@royrodgers has voted on behalf of @minnowpond. If you would like to recieve upvotes from minnowponds team on all your posts, simply FOLLOW @minnowpond.

        To receive an upvote send 0.25 SBD to @minnowpond with your posts url as the memo
        To receive an reSteem send 0.75 SBD to @minnowpond with your posts url as the memo
        To receive an upvote and a reSteem send 1.00SBD to @minnowpond with your posts url as the memo

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.027
BTC 60063.85
ETH 2313.06
USDT 1.00
SBD 2.46