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 is7599+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.