-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhszfc.php
More file actions
63 lines (56 loc) · 1.17 KB
/
hszfc.php
File metadata and controls
63 lines (56 loc) · 1.17 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
<?php
/**
* Created by PhpStorm.
* User: User
* Date: 2017/9/16
* Time: 18:23
*/
function solution($w) {
if($w < 4) {
return 1;//初始字符串定义为122
}
$c = 2;//字符串当前值
$s = array(2);//待转换字符串
$n = 1;//1的数量
$p = 3;//字符串位数
while(true) {
$s_s = array_shift($s);//当前待转字符串
if($s_s == 2) {
if($c == 2) {
$s[] = 1;
$s[] = 1;
$c = 1;
$n = $n + 2;
} else {
$s[] = 2;
$s[] = 2;
$c = 2;
}
$p = $p + 2;
} else {
if($c == 2) {
$s[] = 1;
$c = 1;
$n = $n + 1;
} else {
$s[] = 2;
$c = 2;
}
$p = $p + 1;
}
if($p == $w) {
break;
} else if($p > $w) {
if($c == 1) {
$n = $n - 1;
}
break;
}
}
//var_dump($s);
//echo $p;
//echo $c;
return $n;
}
$line = 19;
echo solution($line);