pku1244 Slots of Fun 简单几何
题目:给出个n行的三角,每个圆里包含一个字母,问哪些字母能够组成等边三角形(每个字母至多出现3次)
解法:
我用了个很懒的方法,算每个圆中心点的坐标,然后看三点距离是否相等。关于求坐标其实还是很简单的- -用向量的方法。
第一行第一个坐标设为(0,0),设置元向量(-0.5,-0.5*sqrt(3)),然后每一行第一个元素坐标就是上行第一个元素坐标+元向量,每一行第i个元素坐标为第i-1个坐标+单位向量。
代码:
1
//
============================================================================
2 // Name : pku1244.cpp
3 // Author : yzhw
4 // Version :
5 // Copyright : yzhw
6 // Description : Hello World in C++, Ansi-style
7 // ============================================================================
8
9 #include < iostream >
10 #include < cstdio >
11 #include < cstring >
12 #include < cmath >
13 #include < algorithm >
14 #include < functional >
15 #include < vector >
16 using namespace std;
17 double x =- 0.5 ,y = x * sqrt( 3.0 );
18 int n;
19 inline double dis(pair < double , double > a,pair < double , double > b)
20 {
21 return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
22 }
23 inline double equ( double a, double b)
24 {
25 return fabs(a - b) < 1e - 6 ;
26 }
27 int main() {
28 double data[ 20 ][ 20 ][ 2 ];
29 while (scanf( " %d " , & n) && n)
30 {
31 char str[ 255 ];
32 vector < pair < double , double > > map[ 26 ];
33 scanf( " %s " ,str);
34 char * p = str;
35 data[ 0 ][ 0 ][ 0 ] = 0 ;
36 data[ 0 ][ 0 ][ 1 ] = 0 ;
37 map[ * (p ++ ) - 97 ].push_back(make_pair < double , double > (data[ 0 ][ 0 ][ 0 ],data[ 0 ][ 0 ][ 1 ]));
38 for ( int i = 1 ;i < n;i ++ )
39 {
40 data[i][ 0 ][ 0 ] = data[i - 1 ][ 0 ][ 0 ] + x;
41 data[i][ 0 ][ 1 ] = data[i - 1 ][ 0 ][ 1 ] + y;
42 map[ * (p ++ ) - 97 ].push_back(make_pair < double , double > (data[i][ 0 ][ 0 ],data[i][ 0 ][ 1 ]));
43 for ( int j = 1 ;j <= i;j ++ )
44 {
45 data[i][j][ 0 ] = data[i][j - 1 ][ 0 ] + 1 ;
46 data[i][j][ 1 ] = data[i][j - 1 ][ 1 ];
47 map[ * (p ++ ) - 97 ].push_back(make_pair < double , double > (data[i][j][ 0 ],data[i][j][ 1 ]));
48 }
49 }
50 bool flag = false ;
51 for ( int i = 0 ;i < 26 ;i ++ )
52 if (map[i].size() == 3 )
53 {
54 if (equ(dis(map[i][ 0 ],map[i][ 1 ]),dis(map[i][ 0 ],map[i][ 2 ])) && equ(dis(map[i][ 0 ],map[i][ 1 ]),dis(map[i][ 1 ],map[i][ 2 ])))
55 {
56 printf( " %c " ,i + 97 );
57 flag = true ;
58 }
59 }
60 if (flag) printf( " \n " );
61 else printf( " LOOOOOOOOSER!\n " );
62 }
63 return 0 ;
64
65 }
66
2 // Name : pku1244.cpp
3 // Author : yzhw
4 // Version :
5 // Copyright : yzhw
6 // Description : Hello World in C++, Ansi-style
7 // ============================================================================
8
9 #include < iostream >
10 #include < cstdio >
11 #include < cstring >
12 #include < cmath >
13 #include < algorithm >
14 #include < functional >
15 #include < vector >
16 using namespace std;
17 double x =- 0.5 ,y = x * sqrt( 3.0 );
18 int n;
19 inline double dis(pair < double , double > a,pair < double , double > b)
20 {
21 return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
22 }
23 inline double equ( double a, double b)
24 {
25 return fabs(a - b) < 1e - 6 ;
26 }
27 int main() {
28 double data[ 20 ][ 20 ][ 2 ];
29 while (scanf( " %d " , & n) && n)
30 {
31 char str[ 255 ];
32 vector < pair < double , double > > map[ 26 ];
33 scanf( " %s " ,str);
34 char * p = str;
35 data[ 0 ][ 0 ][ 0 ] = 0 ;
36 data[ 0 ][ 0 ][ 1 ] = 0 ;
37 map[ * (p ++ ) - 97 ].push_back(make_pair < double , double > (data[ 0 ][ 0 ][ 0 ],data[ 0 ][ 0 ][ 1 ]));
38 for ( int i = 1 ;i < n;i ++ )
39 {
40 data[i][ 0 ][ 0 ] = data[i - 1 ][ 0 ][ 0 ] + x;
41 data[i][ 0 ][ 1 ] = data[i - 1 ][ 0 ][ 1 ] + y;
42 map[ * (p ++ ) - 97 ].push_back(make_pair < double , double > (data[i][ 0 ][ 0 ],data[i][ 0 ][ 1 ]));
43 for ( int j = 1 ;j <= i;j ++ )
44 {
45 data[i][j][ 0 ] = data[i][j - 1 ][ 0 ] + 1 ;
46 data[i][j][ 1 ] = data[i][j - 1 ][ 1 ];
47 map[ * (p ++ ) - 97 ].push_back(make_pair < double , double > (data[i][j][ 0 ],data[i][j][ 1 ]));
48 }
49 }
50 bool flag = false ;
51 for ( int i = 0 ;i < 26 ;i ++ )
52 if (map[i].size() == 3 )
53 {
54 if (equ(dis(map[i][ 0 ],map[i][ 1 ]),dis(map[i][ 0 ],map[i][ 2 ])) && equ(dis(map[i][ 0 ],map[i][ 1 ]),dis(map[i][ 1 ],map[i][ 2 ])))
55 {
56 printf( " %c " ,i + 97 );
57 flag = true ;
58 }
59 }
60 if (flag) printf( " \n " );
61 else printf( " LOOOOOOOOSER!\n " );
62 }
63 return 0 ;
64
65 }
66