首页 > 专题系列 > Java解POJ > POJ 2265 Bee Maja [解题报告] Java
2013
11-11

POJ 2265 Bee Maja [解题报告] Java

Bee Maja

问题描述 :

Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive consists of many hexagonal honey combs where the honey is stored in.

But bee Maja has a problem. Willi told her where she can meet him, but because Willi is a male drone and Maja is a female worker they have different coordinate systems.

Maja’s Coordinate System
Willi’s Coordinate System
Maja who often flies directly to a special honey comb has laid an advanced two dimensional grid over the whole hive.Willi who is more lazy and often walks around just numbered the cells clockwise starting from 1 in the middle of the hive.
MajaWilli


Help Maja to convert Willi’s system to hers. Write a program which for a given honey comb number gives the coordinates in Maja’s system.

输入:

The input contains one or more integers which represent Willi’s numbers. Each number stands on its own in a separate line, directly followed by a newline. The honey comb numbers are all less than 100 000.

输出:

You should output the corresponding Maja coordinates to Willi’s numbers, each coordinate pair on a separate line.

样例输入:

1
2
3
4
5

样例输出:

0 0
0 1
-1 1
-1 0
0 -1

解题代码:

import java.util.Scanner;  
      
    public class Main{  
      
          
        public static void main(String[] args) {  
              
            Scanner scan = new Scanner(System.in);  
              
            while(scan.hasNext()){  
                  
                int n = scan.nextInt();  
                  
                cal(n);  
                  
            }  
      
        }  
      
        public static void cal(int a) {  
              
            if(a == 1){  
                System.out.println(0+" "+0);  
                return ;  
            }  
              
            int n = 0;  
            while(3*(n-1)*n+1< a)      // 3*(n-1)*n+1 表示每层的最后一个数 是谁  
                n++;  
            n--;  
            a -= 3*(n-1)*n+1;    // a现在表示的是a是所在层的第几个数  
            int x , y;  
            if(a<=n){ // 把每一层分为六边,因为本身就是一个正六边形,当在第n层时,每边是有n个点  
                x = n-a;  
                y = a;  
            }else if(a>n&&a<=2*n){  
                x = n-a;  
                y = n;  
            }else if(a>2*n&&a<=3*n){  
                x = -n;  
                y = 3*n-a;  
            }else if(a>3*n&&a<=4*n){  
                x = a-4*n;  
                y = 3*n-a;  
            }else if(a>4*n&&a<=5*n){  
                x = a-4*n;  
                y = -n;  
            }else{  
                x = n;  
                y = a-6*n;  
            }  
              
            System.out.println(x+" "+y);  
              
        }  
      
    }

  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }