【2种方法:纯数学||编程】數學 × 程式編寫比賽 (第八回)math & programe 2 methods for Mathematics × Programming Competition #8

in #cn7 years ago (edited)

With method math and program method to solve Mathematics × Programming Competition #8.用数学方法和编程方法,两种方法,分别解答數學 × 程式編寫比賽 (第八回)

1. Independent math method without program


(1)rotated digits


0 is rotated to 0.
1 is rotated to 1.
2 is rotated to 2.
3 is rotated to nothing.
4 is rotated to nothing.
5 is rotated to 5.
6 is rotated to 9.
7 is rotated to nothing.
8 is rotated to 8.
9 is rotated to 6.

(2)3 situations of number situations


First, the number is rotated to nothing.
Second, the number is rotated to itself. Eg:2112, 6229
Third, the number is rotated to a diffirent number. Eg:1625

(3)Let's count the 3 situations


First, the number is rotated to nothing.


At least one of the 4 digits in the numbers is 3 or 4 or 7.We can count all the 4 digits are neither 3, 4 nor 7.

7X7X7X7=2401

10000-2401=7599


Second, the number is rotated to itself.


We only need to make sure the unit's digit and the ten's digit, then the hundred's digit and thousand's digit are known. The unit's digit is rotated to the thousand's digit, and the ten's digit is rotated to the hundred's digit.

The unit's digit and the ten's digit can ben 0, 1, 2, 5, 6, 8 or 9.

7X7=49


Third, the number is rotated to a diffirent number.


Except fo itseftl, he numer is rotated to a diffirent number.
2401 numbers can be rotated to a mumber, and 49 number is rotated to itself.

2401-49=2352


2352 mubers can be rotated to different mumbers withe themselves.
But half of the 2352 numbers are be rotated to the other half.

2352/2=1175


The final result is

7599+49+1176=8824

(一)纯数学方法

1数字的翻转

0翻转:0
1翻转:1
2翻转:2
3翻转:无
4翻转:无
5翻转:5
6翻转:9
7翻转:无
8翻转:8
9翻转:6

2组合情况


在0000-9999这10000个数分三种情况:
第一种:翻转以后,就不再是数。
第二种:翻转以后,还是它自己。例如:2112、6229
第三种:翻转以后,变成另一个数。例如1625

3分别计算三种情况:


第一种:翻转不是数。


那说明,四个数字中,至少有一个数字是3或4或7

四个数字全部都不是3、4、7的话,共有

7X7X7X7=2401个


那么,至少有一个数字是3或4或7的情况

=10000-2401=7599

第二种:翻转以后,还是它自己。


其实,只要看前2位就行了,只要前2位确定了,个位数字应该是千数数字翻转,十位数字是百位数字翻转,只要这样,这个数翻转就还是它自己。前两位数可以是0、1、2、5、6、8、9中的任意两个,所以共有:

7X7=49种情况。

第三种:翻转以后,变成另一个数。


前面计算了,四个数字全部都不是3、4、7的话共有2401种情况,这2401个数翻转还是一个正常的数。
那么,这个数翻转以后,不是它自己,就是另一个种,翻转是另一个数的情况

=2401-49=2352


注意了,这2352个数中,是两两对应的,这两个数就是彼此翻转的结果,例如1625与5291
所以,应该有:

2352/2=1176种情况。

所以,这道题最后的答案

=7599+49+1176=8824

(二)编程方法 Program method

<?php
$arr[0]=0;                         //To save the digit and the rotated digit into the subscript and the value of array.
$arr[1]=1;
$arr[2]=2;
$arr[5]=5;
$arr[6]=9;
$arr[8]=8;
$arr[9]=6;      

$n=0;                                // To count the numbers.
for($i=0; $i<10000;$i++)
{
    $dig_4=floor($i/1000);           // To calculate the thousand's digit.
    if(!in_array($dig_4,$arr))       // If the thousand's digit is not in the array, "i" cann't be rotated to a number.
    {
        $n++;
    }
    else
    {
        $dig_3=floor($i/100)%10;       // To calculate the hundred's digit.
        if(!in_array($dig_3,$arr))     // If the hundred's digit is not in the array, "i" cann't be rotated to a number.
        {
            $n++;
        }
        else
        {
            $dig_2=floor($i/10)%10;      // To calculate the ten's digit.
            if(!in_array($dig_2,$arr))   // If the ten's digit is not in the array, "i" cann't be rotated to a number.
            {
                $n++;
            }
            else
            {
                $dig_1=$i%10;              // To calculate the unit's digit.
                if(!in_array($dig_1,$arr)) // If the unit's digit is not in the array, "i" cann't be rotated to a number.
                {
                    $n++;
                }
                else                       // If each digit is in the array, "i" can be rotated to a number.
                {
                    $r=0;
                    $r+=1000*$arr[$dig_1];   // To calculate the rotated number. 
                    $r+=100*$arr[$dig_2];
                    $r+=10*$arr[$dig_3];
                    $r+=$arr[$dig_4]; 
                    if($i<=$r)               // For eliminating duplicate.                    
                    {
                        $n++;
                    }
                }
            }
        }
    }
}
echo $n; 
?>

感谢 @kenchung@steemstem 举办这么好的活动。

Thanks @kenchung & @steemstem for the excellent contest.

Sort:  

得烧死多少脑细胞,TVB女士真是牛得可以。

不牛不牛,我每次算法都有点问题,所以要想办法在数学上补一补。烧脑确实是,不过可以预防老年痴呆。

哈哈,这话我好像哪里听过。

哪句,预防老年痴呆?

这机器人回得太快了吧, STEEMSQL还得10分钟同步呢。
是的。

不是呀,前阵子就不好,这阵子又好用了,有消息就会提示,弹出一个小框呀,好像是桌面通知。

这是极好的。

感觉姐什么都会😘太厉害了

不会画画

I wrote an article about the stephen chow (TVB 周星驰 ) movie, can you read this article? I believe you will like it^^
https://steemit.com/cn/@a-alice/blog-24-review-movie-tvb-stephen-chow-s-best-films

哈哈好喜歡那張gif圖呢

哈哈好玩吧

目前见到最好的答案,佩服

谢谢,我编程的算法还有改进的可能我觉得

厉害了我的姐!!

不敢不敢

tvb 姐诗写得好, 代码写的也好~

我诗真心一般,代码更不行,我往期的往法都有问题,这回这个也还可以改进的,因为效率上还是有问题的,我是懒得改了。

你能不能夸夸我的数学,貌似在这期大家发的答案里,这个数学解法还算比较好玩。

有想過數學方法,但最後還是太懶了,用excel暴力拆解就好了。

编程其实就是暴力方法吧,一个个试,吼吼

哈哈是的,想問一下你的編程是用那種語言~

<?php ?>

上面有的,哈哈哈

Excellent logical thinking and explanation.

Thanks for your praise!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.031
BTC 61179.86
ETH 2631.51
USDT 1.00
SBD 2.46