Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71872
  • 博文数量: 41
  • 博客积分: 1475
  • 博客等级: 上尉
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 22:49
文章分类
文章存档

2012年(8)

2011年(1)

2009年(32)

我的朋友
最近访客

分类:

2009-04-16 20:44:47

/*
 * UVA Problem: 10182
 * Xiaofeng Ye
 *
 * 1 round 0
 * 2---7 round 1
 * 8---19 round 2
 * 20--37 round 3
 * 38--61 round 4
 *
 * The last number of the round = 3*round(round+1)+1
 * eg: 7=1*3(1+1)+1
 */



#include <math.h>
#include <stdio.h>

int get_the_round(int willi_pos);
int get_pos_in_round(int willi_pos, int round);

struct position {
    int x;
    int y;
};

struct position pos_1to7[7] = {
    {0,0}, {0, 1}, {-1,1}, {-1,0}, {0,-1}, {1,-1}, {1,0}
};

#define UP_LEFT(pos) (pos->x)--
#define UP(pos) (pos->y)--
#define UP_RIGHT(pos) ((pos->)x++; (pos->y)--)
#define DOWN_RIGHT(pos) (pos->x)++
#define DOWN(pos) (pos->y)++
#define DOWN_LEFT(pos) ((pos->x)--; (pos->y)++)

int main(int argc, char **argv)
{
    int the_round;
    int willi_pos;
    int i;
    struct position pos2; /* The position of 2 */
    struct position final_pos;
    struct position *p_final_pos = &final_pos;
    int pos_in_round;
    int n;
    int found_flag = 0;

    pos2.x = 0;
    pos2.y = 1;
    final_pos = pos2;
    while (!feof(stdin)) {
        scanf("%d", &willi_pos);
        the_round = get_the_round(willi_pos);
        pos_in_round = get_pos_in_round(willi_pos, the_round));
        for (i = 0; i < the_round-1; i++) {
            DOWN_RIGHT(p_final_pos);
        }
        if (1 == pos_in_round) {
            printf("%d %d\n", p_final_pos->x, p_final_pos->y);
            continue;
        }
        n = 0;
        for (i = 0; i < the_round-1; i++) {
            DOWN_LEFT(p_final_pos);
            n++;
            if (n == pos_in_round-1) {
                printf("%d %d\n", p_final_pos->x, p_final_pos->y);
                break;
            }
        }
    }
}

int get_the_round(int willi_pos)
{
    int tmp;

    if (willi_pos == 1) {
        return 0;
    }
    tmp = ceil((ceil(sqrt(12*(willi_pos-1)+9))-3)/6);
    if (3*tmp*(tmp+1)+1 < willi_pos) {
        return tmp+1;
    } else if (3*(tmp-1)*tmp+1 < willi_pos) {
        return tmp;
    }
}

int get_pos_in_round(int willi_pos, int round)
{
    if (round == 0) {
        return 1;
    }
    return willi_pos - (3*(round-1)*round+1);
}

阅读(353) | 评论(1) | 转发(0) |
0

上一篇:ACM UVA (10181)

下一篇:ACM UVA (10138)

给主人留下些什么吧!~~

chinaunix网友2011-04-17 12:05:02

您可以把它转换成 Pascal 代码吗?