1991 ACM Scholastic Programming Contest Finals
sponsored by AT&T Computer Systems
Problem B
Triangular Vertices
Consider the points on an infinite grid of equilateral triangles as shown below:
Note that if we number the points from left to right and top to bottom, then groups of these points form the vertices of certain geometric shapes. For example, the sets of points {1,2,3} and {7,9,18} are the vertices of triangles, the sets {11,13,26,24} and {2,7,9,18} are the vertices of parallelograms, and the sets {4,5,9,13,12,7} and {8,10,17,21,32,34} are the vertices of hexagons.
Write a program which will repeatedly accept a set of points on this triangular grid, analyze it, and determine whether the points are the vertices of one of the following "acceptable" figures: triangle, parallelogram, or hexagon. In order for a figure to be acceptable, it must meet the following two conditions:
- Each side of the figure must coincide with an edge in the grid.
- All sides of the figure must be of the same length.
Input and Output
The input will consist of an unknown number of point sets. Each point set will appear on a separate line in the file. There are at most six points in a set and the points are limited to the range 1..32767.
For each point set in the input file, your program should deduce from the number of points in the set which geometric figure the set potentially represents; e.g., six points can only represent a hexagon, etc. The output must be a series of lines listing each point set followed by the results of your analysis.
Sample Input
1 2 3
11 13 29 31
26 11 13 24
4 5 9 13 12 7
1 2 3 4 5
47
11 13 23 25
Sample Output
1 2 3 are the vertices of a triangle
11 13 29 31 are not the vertices of an acceptable figure
26 11 13 24 are the vertices of a parallelogram
4 5 9 13 12 7 are the vertices of a hexagon
1 2 3 4 5 are not the vertices of an acceptable figure
47 are not the vertices of an acceptable figure
11 13 23 25 are not the vertices of an acceptable figure
Triangular Vertices
/* linhanjie 2008-09-24*/
#include <stdio.h> #include <stdlib.h> #include <string.h>
#include <assert.h>
struct triangular_info { int level; int max; };
int level(int a, struct triangular_info *ti) { int start = 1; int border = 0; for(int i=1;;i++) { if(a>=start && a<=start+border) { ti->level = i; ti->max = start+border; return 0; } start += border+1; border++; } return -1; }
int distance(int a, int b) { struct triangular_info ti; if(level(a, &ti) == -1) { printf("level(%d) error\n", a); return -1; } int distance = 0; if(b <= ti.max)return b-a; int temp = ti.level; int i,j; for(i=a+temp, j=1;;temp++, i+=temp, j++) { if(b == i)return j; if(b < i)break; } temp = ti.level; temp++; for(i=a+temp, j=1;;temp++, i+=temp, j++) { if(b == i)return j; if(b < i)break; } return -1; }
void check(int *a) {
int num = 0; int i,j; for(i=0; i<6; i++) { if(a[i] != 0)num++; } if(num != 3 && num != 4 && num != 6) { printf("THis is not a acceptable figure\n"); return; }
for(i=0; i<num-1; i++) { for(j=i+1; j<num; j++) { if(a[j] < a[i]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } }
if(num == 3) { int b1,b2,b3; b1 = distance(a[0], a[1]); b2 = distance(a[0], a[2]); b3 = distance(a[1], a[2]); if(b1==b2 && b1==b3) { printf("This is a triangular\n"); } else { printf("This is not a triangular\n"); } } else if(num == 4) { int b1,b2,b3,b4; b1 = distance(a[0], a[1]); b2 = distance(a[0], a[2]); b3 = distance(a[1], a[3]); b4 = distance(a[2], a[3]); printf("%d %d %d %d\n", b1, b2, b3, b4); if(b1==b4 && b2==b3) { printf("This is a parallelogram\n"); } else { printf("This is not a parallelogram\n"); } } else if(num == 6) { int b1,b2,b3,b4,b5,b6; int c1,c2,c3;
b1 = distance(a[0], a[1]); b2 = distance(a[0], a[2]); b3 = distance(a[1], a[3]); b4 = distance(a[2], a[4]); b5 = distance(a[4], a[5]); b6 = distance(a[3], a[5]);
c1 = distance(a[0], a[5]); c2 = distance(a[1], a[4]); c3 = distance(a[2], a[3]);
if(b1==b2 && b1==b3 && b1==b4 && b1==b5 && b1==b6 && c1 == c2 && c1 == c3) { printf("This is a hexagon\n"); } else { printf("This is not a hexagon\n"); } } }
int main() {
char buf[1024]; int a[6]; for(;;) { memset(a, 0, 6*4); if(fgets(buf, 1024, stdin) == NULL)return -1; sscanf(buf, "%d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]); // assert(a[0] > 0 && a[1] > 0 &&a[2] > 0 &&a[3] > 0 &&a[4] > 0);
check(a); } return 0; }
|
阅读(1683) | 评论(0) | 转发(1) |