-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuity_num.php
More file actions
77 lines (64 loc) · 1.77 KB
/
continuity_num.php
File metadata and controls
77 lines (64 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Created by PhpStorm.
* User: User
* Date: 2017/8/14
* Time: 11:06
*/
$number = '';
function getNum($s, $e) {
global $number;
$f = '';
for($i=$s;$i<=$e;$i++) {
$f .= $number[$i];
}
return $f;
}
function solution($line) {
global $number;
$number = $line;
$flag = 'false';
$l = strlen($number);
$w = ceil($l/2);
for($i=0;$i<$w;$i++) {
for($j=($i+1);$j<$w;$j++) {
$f = getNum(0, $i);
$s = getNum($i+1, $j);
$t_j = $j;
while (true) {
$t_f_s = $f * $s;
//echo $f.'*'.$s.'='.$t_f_s."\n";
$t_l = strlen($t_f_s);
$t_e = $t_j + $t_l;
if($t_e >= $l) {
break;
}
$t_f_s_s = getNum($t_j+1, $t_e);
if($t_f_s_s == $t_f_s) {
if($t_e == $l-1) {
$flag = 'true';
break 3;
} else {
$f = $s;
$s = $t_f_s_s;
$t_j = $t_e;
}
} else {
break;
}
}
}
//echo $i."\n";
//break;
}
return $flag;
}
$line = '122';
$flag = solution($line);
echo $flag;
/**
* 没有太多心得体会,是做得最快的一个题目,一气呵成,没有bug
* 主要运用了常规的解题思想,用纸笔推算的顺序、多次尝试
* 妙的事用程序准确的反映了解题思路,最后用了折半退出的问题点,提高了性能(如果推演到数字字符串的一半,还是不能连乘,即退出,因为后面的位数小于前面的永不可能相等)
*
*/