歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
經典的遞歸問題,遞歸+回溯解決。
注意代碼中select_combination()的應用
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
// POJ 2245
// Tanky Woo
// www.wutianqi.com
#include<iostream>
#include<algorithm>
using namespace std;
int num[14];
int lotto[14];
int n,cnt;
void select_combination(int current,int p)//遞歸填充數并打
印組合,相當于DFS過程
{
int i;
if(6 == current)
{
for(i = 0; i < 6; ++i)
printf("%d ", lotto[i]);
printf("\n");
}
else
{
for(i = p; i < n; ++i)
{
lotto[current] = num[i];
select_combination(current+1,
i+1);
}
}
}
// www.wutianqi.com
int main()
{ [...]
文章來源:
http://www.wutianqi.com/?p=287
posted @
2010-07-08 18:33 Tanky Woo 閱讀(146) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
經典的遞歸問題,遞歸+回溯解決。
注意代碼中select_combination()的應用
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
// POJ 2245
// Tanky Woo
// www.wutianqi.com
#include<iostream>
#include<algorithm>
using namespace std;
int num[14];
int lotto[14];
int n,cnt;
void select_combination(int current,int p)//遞歸填充數并打
印組合,相當于DFS過程
{
int i;
if(6 == current)
{
for(i = 0; i < 6; ++i)
printf("%d ", lotto[i]);
printf("\n");
}
else
{
for(i = p; i < n; ++i)
{
lotto[current] = num[i];
select_combination(current+1,
i+1);
}
}
}
// www.wutianqi.com
int main()
{ [...]
文章來源:
http://www.wutianqi.com/?p=287
posted @
2010-07-08 18:33 Tanky Woo 閱讀(84) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
很經典的遞歸!
把握前序,中序,后序的特點。
前序的第一個元素是樹的根,在相應中序中根以左是左子樹,根以右是右子樹。
注意考慮 n == 1和 n == 0 的情況。
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
// POJ 2255 Tree Recovery
// Tanky Woo
// Memory: 180K Time: 0MS
// Language: C++ Result: Accepted
#include <iostream>
#include <string>
using namespace std;
//定義前序,中序,后序序列
string preord, inord, postord;
void find_postord(string preord, string inord);
// Blog:www.wutianqi.com
int main()
{
while(cin >> preord >> inord)
{
find_postord(preord, inord);
cout << endl;
}
return 0;
}
//找到ch在inord中的位置,未找到返回-1
int find_root(string inord, char ch)
{
int i;
for(i = 0; [...]
文章來源:
http://www.wutianqi.com/?p=285
posted @
2010-07-08 18:27 Tanky Woo 閱讀(59) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
很經典的遞歸!
把握前序,中序,后序的特點。
前序的第一個元素是樹的根,在相應中序中根以左是左子樹,根以右是右子樹。
注意考慮 n == 1和 n == 0 的情況。
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
// POJ 2255 Tree Recovery
// Tanky Woo
// Memory: 180K Time: 0MS
// Language: C++ Result: Accepted
#include <iostream>
#include <string>
using namespace std;
//定義前序,中序,后序序列
string preord, inord, postord;
void find_postord(string preord, string inord);
// Blog:www.wutianqi.com
int main()
{
while(cin >> preord >> inord)
{
find_postord(preord, inord);
cout << endl;
}
return 0;
}
//找到ch在inord中的位置,未找到返回-1
int find_root(string inord, char ch)
{
int i;
for(i = 0; [...]
文章來源:
http://www.wutianqi.com/?p=285
posted @
2010-07-08 18:27 Tanky Woo 閱讀(150) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
?誰說這道題簡單?是水題?
又讓我郁悶了。
思路:枚舉,BFS,位操作
分析:要求輸入一個4*4的棋盤,考慮狀態是否全為黑或者全為白,狀態共有2^16種,聯系棋盤是4*4,可以想到用位來壓縮狀態。
bwwb
bbwb
bwwb
bwww
可以表示為:0001|1001|1011|1001
這里對位操作介紹下:
id ^= (1 << i)?? //對整數id轉換成二進制后的第i位取反
其次,這里是廣搜,用隊列表示。
queue的front(), pop(), push()要掌握。
題目地址:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1753
?Memory:?504K
?
?
?Time:?16MS
?
?Language:?C++
?
?
?Result:?Accepted
自定義難度:★★★☆☆
代碼:
?
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
78
79
80
81
82
83
84
85
86
87
88
// POJ 1753
// Author: Tanky Woo
#include <iostream>
#include <queue>
using namespace std;
const int MAX_STATE = 65536;
const int ALL_BLACK = 65535;
const int ALL_WHITE = 0;
const int SIZE = 4;
// www.wutianqi.com
int convert(char c)
{
if(c == 'b')
return 1;
else
return 0;
}
int FlipPiece(int state_id, int pos)
{
state_id ^= (1 << pos);
//up
if(pos >= 4)
state_id [...]
文章來源:
http://www.wutianqi.com/?p=280
posted @
2010-07-06 18:37 Tanky Woo 閱讀(1125) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
?誰說這道題簡單?是水題?
又讓我郁悶了。
思路:枚舉,BFS,位操作
分析:要求輸入一個4*4的棋盤,考慮狀態是否全為黑或者全為白,狀態共有2^16種,聯系棋盤是4*4,可以想到用位來壓縮狀態。
bwwb
bbwb
bwwb
bwww
可以表示為:0001|1001|1011|1001
這里對位操作介紹下:
id ^= (1 << i)?? //對整數id轉換成二進制后的第i位取反
其次,這里是廣搜,用隊列表示。
queue的front(), pop(), push()要掌握。
題目地址:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1753
?Memory:?504K
?
?
?Time:?16MS
?
?Language:?C++
?
?
?Result:?Accepted
自定義難度:★★★☆☆
代碼:
?
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
78
79
80
81
82
83
84
85
86
87
88
// POJ 1753
// Author: Tanky Woo
#include <iostream>
#include <queue>
using namespace std;
const int MAX_STATE = 65536;
const int ALL_BLACK = 65535;
const int ALL_WHITE = 0;
const int SIZE = 4;
// www.wutianqi.com
int convert(char c)
{
if(c == 'b')
return 1;
else
return 0;
}
int FlipPiece(int state_id, int pos)
{
state_id ^= (1 << pos);
//up
if(pos >= 4)
state_id [...]
文章來源:
http://www.wutianqi.com/?p=280
posted @
2010-07-06 18:37 Tanky Woo 閱讀(112) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
今天論壇的ambition發了一個帖子:
http://www.cppleyuan.com/redirect.php?tid=1171&goto=lastpost#lastpost
看了下,感覺題目很不錯,ambition的思路也很好。
涉及到了m^n,我感覺經常就是與log一起使用。
把題目做了下,ambition的代碼已經足夠了,貼出來分享:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,m;
double g;
double h;
for(cin>>n;n>0;n--)
{
cin>>m;
g=m*log10((double)m);
[...]
文章來源:
http://www.wutianqi.com/?p=278
posted @
2010-07-05 23:17 Tanky Woo 閱讀(137) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
今天論壇的ambition發了一個帖子:
http://www.cppleyuan.com/redirect.php?tid=1171&goto=lastpost#lastpost
看了下,感覺題目很不錯,ambition的思路也很好。
涉及到了m^n,我感覺經常就是與log一起使用。
把題目做了下,ambition的代碼已經足夠了,貼出來分享:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,m;
double g;
double h;
for(cin>>n;n>0;n--)
{
cin>>m;
g=m*log10((double)m);
[...]
文章來源:
http://www.wutianqi.com/?p=278
posted @
2010-07-05 23:17 Tanky Woo 閱讀(127) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
就算是第一題又如何,POJ 1000,這是每個ACM愛好者的第一次。。。。
今天打擊很大,三個小時2題都沒做出來,感覺以前做的太混亂了,
今天重新申請了一個號,開始新的旅程,第一站當然就是這一題。
大家可以去看看我的代碼情況,我賬號:200841193
從今天開始系統的開始做了。加油!Tanky Woo!
題目地址:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1000
1
2
3
4
5
6
7
8
9
10
11
12
//Author: Tanky Woo
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
歡迎您來到C++奮斗樂園,原創文章,轉載請注明: 轉載自Tanky Woo 的程序人生
文章標題: POJ 1000 A+B Problem
本文鏈接地址: http://www.wutianqi.com/?p=276
文章來源:
http://www.wutianqi.com/?p=276
posted @
2010-07-05 21:14 Tanky Woo 閱讀(114) |
評論 (0) |
編輯 收藏
歡迎您來到Tanky Woo的博客:
我們的【C++奮斗樂園】
C++/算法網站:www.cpply.com
C++/算法論壇:www.cppleyuan.com
QQ群:①群:19333724 ②群:23840480 ③群:17314377 ④群:23829384
就算是第一題又如何,POJ 1000,這是每個ACM愛好者的第一次。。。。
今天打擊很大,三個小時2題都沒做出來,感覺以前做的太混亂了,
今天重新申請了一個號,開始新的旅程,第一站當然就是這一題。
大家可以去看看我的代碼情況,我賬號:200841193
從今天開始系統的開始做了。加油!Tanky Woo!
題目地址:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1000
1
2
3
4
5
6
7
8
9
10
11
12
//Author: Tanky Woo
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
歡迎您來到C++奮斗樂園,原創文章,轉載請注明: 轉載自Tanky Woo 的程序人生
文章標題: POJ 1000 A+B Problem
本文鏈接地址: http://www.wutianqi.com/?p=276
文章來源:
http://www.wutianqi.com/?p=276
posted @
2010-07-05 21:14 Tanky Woo 閱讀(195) |
評論 (1) |
編輯 收藏