无聊题.调节心情.
Code
1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5const float PI = 3.1415927;
6const int FEET_MILE = 5280;
7const int INCH_FOOT = 12;
8const int MINUTES_HOUR = 60;
9const int SECONDS_MINUTE = 60;
10const float METERS_FURLONG = 201.168f;
11
12int _tmain(int argc, _TCHAR* argv[])
13{
14 float diameter, time;
15 int revolutions;
16
17 int index = 1;
18
19 while(cin >> diameter >> revolutions >> time && revolutions != 0)
20 {
21 float mile = 2 * PI * diameter / 2 * revolutions / INCH_FOOT / FEET_MILE;
22
23 float MPH = mile / (time / SECONDS_MINUTE / MINUTES_HOUR);
24
25 cout << "Trip #" << index << ": " << fixed << setprecision(2) << mile << " " << MPH << endl;
26 ++index;
27 }
28
29 return 0;
30}
posted @
2009-04-10 22:42 肖羽思 阅读(607) |
评论 (0) |
编辑 收藏
Prim算法.
Code
1#include <iostream>
2using namespace std;
3
4const int N = 28;
5const int UNLINK = 0x7fffffff;
6int g[N][N];
7int weight[N];
8bool visited[N];
9
10int _tmain(int argc, _TCHAR* argv[])
11{
12 int vertex, t_vertex;
13 while(cin >> t_vertex && t_vertex != 0)
14 {
15 vertex = t_vertex;
16 memset(visited, false, sizeof(visited));
17
18 for(int i = 0; i < vertex; ++i)
19 {
20 weight[i] = UNLINK;
21 for(int j = 0; j < vertex; ++j)
22 {
23 g[i][j] = UNLINK;
24 }
25 }
26
27 char v;
28 int num, t_num;
29 while(--t_vertex)
30 {
31 cin >> v >> t_num;
32 num = t_num;
33
34 char vl;
35 int edge;
36 while(t_num--)
37 {
38 cin >> vl >> edge;
39 g[(int)(v - 'A')][(int)(vl - 'A')] = edge;
40 g[(int)(vl - 'A')][(int)(v - 'A')] = edge;
41 }
42 }
43
44 for(int i = 0; i < vertex; ++i)
45 {
46 weight[i] = g[0][i];
47 }
48 visited[0] = true;
49 int min(UNLINK), nearest(-1), total_weight(0);
50
51 for(int i = 0; i < vertex; ++i)
52 {
53 min = UNLINK;
54 nearest = -1;
55 for(int j = 0; j < vertex; ++j)
56 {
57 if(min > weight[j] && !visited[j])
58 {
59 min = weight[j];
60 nearest = j;
61 }
62 }
63 visited[nearest] = true;
64 total_weight += weight[nearest];
65
66 for(int j = 0; j < vertex; ++j)
67 {
68 if(g[nearest][j] < weight[j])
69 {
70 weight[j] = g[nearest][j];
71 }
72 }
73 }
74
75 cout << total_weight << endl;
76 }
77 return 0;
78}
79
80
posted @
2009-04-10 22:41 肖羽思 阅读(823) |
评论 (0) |
编辑 收藏
简单题.
Code
1#include <iostream>
2using namespace std;
3
4int _tmain(int argc, _TCHAR* argv[])
5{
6 int cases;
7 cin >> cases;
8
9 while(cases--)
10 {
11 int n;
12 cin >> n;
13
14 int * cells = new int[n + 1];
15 for(int i = 0; i <= n; ++i)
16 {
17 cells[i] = 0;
18 }
19
20 for(int i = 2; i <= n; ++i)
21 {
22 if(i > n / 2)
23 break;
24 for(int j = i; j <= n; j += i)
25 {
26 ++cells[j];
27 }
28 }
29
30 int result = 0;
31
32 for(int i = 1; i <= n / 2; ++i)
33 {
34 if((cells[i] & 1) == 0)
35 ++result;
36 }
37
38 for(int i = n / 2 + 1; i <= n; ++i)
39 {
40 if((cells[i] & 1) == 1)
41 ++result;
42 }
43 cout << result << endl;
44 delete cells;
45 }
46 return 0;
47}
48
49
posted @
2009-04-10 22:38 肖羽思 阅读(627) |
评论 (0) |
编辑 收藏
经典题.双重BFS.
基本思路如下:
1.第一重BFS根据箱子可移动的位置进行BFS.
2.第二重将箱子目前所在的位置设为不可达,根据箱子所在的位置得出人所应该在的位置,根据此位置对人进行了BFS.
即可.
posted @
2009-04-10 22:36 肖羽思 阅读(470) |
评论 (0) |
编辑 收藏
经典题.双重BFS.
基本思路如下:
1.第一重BFS根据箱子可移动的位置进行BFS.
2.第二重将箱子目前所在的位置设为不可达,根据箱子所在的位置得出人所应该在的位置,根据此位置对人进行了BFS.
即可.
posted @
2009-04-10 22:33 肖羽思 阅读(533) |
评论 (0) |
编辑 收藏
简单题.面试的时候常考.
Code
1#include <iostream>
2#include <string>
3using namespace std;
4
5int _tmain(int argc, _TCHAR* argv[])
6{
7 int t;
8 cin >> t;
9 for(int g = 0; g < t; ++g)
10 {
11 if(g != 0)
12 cout << endl;
13
14 int n;
15 cin >> n;
16 getchar();
17
18 string res, r_res;
19
20 for(int i = 0; i < n; ++i)
21 {
22 getline(cin, res);
23 int length = res.length();
24 int last_space = -1;
25
26 for(int i = 0; i <= length; ++i)
27 {
28 if(i == length || res.at(i) == ' ')
29 {
30 for(int j = i - 1; j > last_space; --j)
31 {
32 cout << res.at(j);
33 }
34 if(i != length)
35 cout << " ";
36 last_space = i;
37 }
38 }
39 cout << endl;
40 }
41 }
42 return 0;
43}
44
45
posted @
2009-04-10 22:24 肖羽思 阅读(813) |
评论 (0) |
编辑 收藏
无聊题.缓解心情.
Code
1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5const int N = 9;
6
7int factorial(int n)
8{
9 if(n == 0)
10 return 1;
11 return n * factorial(n - 1);
12}
13
14int _tmain(int argc, _TCHAR* argv[])
15{
16 cout << "n e" << endl;
17 cout << "- -----------" << endl;
18
19 double result = 0.0f, temp;
20
21 for(int i = 0; i <= N; ++i)
22 {
23 result = 0.0f;
24
25 for(int j = i; j > -1; --j)
26 {
27 result += 1.0 / factorial(j);
28 }
29
30 if(i > 2)
31 {
32 printf("%d %.9f\n", i, result);
33 }
34 else
35 {
36 cout << i << " " << result << endl;
37 }
38 }
39 return 0;
40}
posted @
2009-04-10 22:21 肖羽思 阅读(575) |
评论 (1) |
编辑 收藏
无聊题.用于缓解心情.
Code
1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5const int N = 9;
6
7int factorial(int n)
8{
9 if(n == 0)
10 return 1;
11 return n * factorial(n - 1);
12}
13
14int _tmain(int argc, _TCHAR* argv[])
15{
16 cout << "n e" << endl;
17 cout << "- -----------" << endl;
18
19 double result = 0.0f, temp;
20
21 for(int i = 0; i <= N; ++i)
22 {
23 result = 0.0f;
24
25 for(int j = i; j > -1; --j)
26 {
27 result += 1.0 / factorial(j);
28 }
29
30 if(i > 2)
31 {
32 printf("%d %.9f\n", i, result);
33 }
34 else
35 {
36 cout << i << " " << result << endl;
37 }
38 }
39 return 0;
40}
posted @
2009-04-10 22:17 肖羽思 阅读(459) |
评论 (0) |
编辑 收藏
利用
大数类简单解决的简单题.
Code
1#include "BigInteger.h"
2#include <iostream>
3#include <vector>
4#include <string>
5#include <cmath>
6#include <iomanip>
7using namespace std;
8
9bool IsCyclic(string input, string result)
10{
11 int length = input.length();
12 int flag[100] = {0};
13 for(int i = 0; i < length; ++i)
14 {
15 for(int j = 0; j < length; ++j)
16 {
17 if(!flag[j] && input.at(i) == result.at(j))
18 {
19 flag[j] = 1;
20 break;
21 }
22 }
23 }
24
25 for(int i = 0; i < length; ++i)
26 {
27 if(!flag[i])
28 return false;
29 }
30 return true;
31}
32
33
34int _tmain(int argc, _TCHAR* argv[])
35{
36 string input;
37 while(cin >> input)
38 {
39 int length = input.length();
40 BigInteger integer(input);
41 BigInteger result(1);
42 bool isCyclic = true;
43 for(int i = 2; i < length + 1; ++i)
44 {
45 result = integer * BigInteger(i);
46 if(!IsCyclic(input, result.GetString()))
47 {
48 isCyclic = false;
49 break;
50 }
51 }
52 if(isCyclic)
53 cout << input << " is cyclic" << endl;
54 else
55 cout << input << " is not cyclic" << endl;
56 }
57 return 0;
58}
59
posted @
2009-04-10 22:15 肖羽思 阅读(717) |
评论 (1) |
编辑 收藏
简单模拟题.
Code
1#include <iostream>
2using namespace std;
3
4int _tmain(int argc, _TCHAR* argv[])
5{
6 int n;
7
8 bool reInput = false;
9
10 while(cin >> n && n != 0)
11 {
12 if(reInput)
13 cout << endl;
14
15 reInput = true;
16
17 int *A = new int[n];
18 int *B = new int[n];
19
20 for(int i = 0; i < n; ++i)
21 {
22 cin >> A[i];
23 }
24
25 for(int i = 0; i < n; ++i)
26 {
27 cin >> B[i];
28 }
29
30 int score_A = 0;
31 int score_B = 0;
32
33 for(int i = 0; i < n; ++i)
34 {
35 if(A[i] > B[i])
36 {
37 if(A[i] - B[i] == 1)
38 {
39 if(A[i] == 2)
40 {
41 score_B += 6;
42 }
43 else
44 {
45 score_B += A[i];
46 score_B += B[i];
47 }
48 }
49 else
50 {
51 score_A += A[i];
52 }
53 }
54 else if(A[i] < B[i])
55 {
56 if(B[i] - A[i] == 1)
57 {
58 if(B[i] == 2)
59 {
60 score_A += 6;
61 }
62 else
63 {
64 score_A += A[i];
65 score_A += B[i];
66 }
67 }
68 else
69 {
70 score_B += B[i];
71 }
72 }
73 }
74
75 cout << "A has " << score_A << " points. B has " << score_B << " points." << endl;
76
77 delete A, B;
78 }
79
80 return 0;
81}
82
83
posted @
2009-04-10 22:10 肖羽思 阅读(481) |
评论 (0) |
编辑 收藏