程序员2008.1算法题

/**
 * Cantor表:
 * 有理数可以按照以下顺序进行排列,这个排列表叫Cantor Table
 *   1/1 1/2 1/3 1/4
 *   2/1 2/2 2/3 2/4
 *   3/1 3/2 3/3 3/4
 *   4/1 4/2 4/3 4/4
 *    …………
 * 可以对Cantor表进行序列化:
 *   1/1 1/2 2/1 3/1 2/2 1/3 1/4 ……
 * 给出一个Cantor取出它在序列中的位置,或通过位置求节点的值
 */

import  java.io. * ;
import  java.util.ArrayList;

class  CantorNode  {

    
/**
     * 除数
     
*/

    
private int _nominator;

    
/**
     * 被除数
     
*/

    
private int _denominator;

    
public CantorNode(int nominator, int denominator) {
        
this._nominator = nominator;
        
this._denominator = denominator;
    }


    
public int getNominator() {
        
return _nominator;
    }


    
public int getDenominator() {
        
return _denominator;
    }


    
/**
     * 判断两个CantorNode是否相等
     
*/

    
public boolean equals(CantorNode node) {
        
return (this.getNominator() == node.getNominator() && this
                .getDenominator() 
== node.getDenominator());
    }


    
/**
     * 转化为分数形式的字符串
     
*/

    
public String toString() {
        
return this.getNominator() + "/" + this.getDenominator();
    }


}


public   class  Cantor  {

    
/**
     * 通过序列中的序号计算节点
     * 
     * 
@param index
     * 
@return
     
*/

    
public static CantorNode getCantorNode(int index) {
        
if (index < 1{
            
return null;
        }
 else {
            
// 主计算过程
            int t = (int) Math.sqrt(index * 2);
            
long t1 = t * (t - 1/ 2;
            
while (t1 + t + 1 < index) {
                t
++;
                t1 
= t * (t - 1/ 2;
            }

            
// 计算结果处理
            int m = (int) (index - t1);
            
int n = (int) (t + 1 - index + t1);
            
if ((t & 1== 0{
                
return new CantorNode(m, n);
            }
 else {
                
return new CantorNode(n, m);
            }

        }

    }


    
/**
     * 主函数入口
     
*/

    
public static void main(String[] args) {

        ArrayList
<Integer> list = new ArrayList<Integer>();

        
// 获取输入
        while (true{
            
try {
                BufferedReader br 
= new BufferedReader(new InputStreamReader(
                        System.in));
                String input 
= br.readLine();
                
int in = Integer.parseInt(input);
                
// 输入不符合要求,跳过,继续获取输入
                if (in < 0 || in > 100000)
                    
continue;
                list.add(in);
                
// 退出,开始计算
                if (in == 0)
                    
break;
            }
 catch (IOException e) {
                
break;
            }

        }


        
for (int i = 0; i < list.size(); i++{
            
// 遍历并计算输入序列对应的输出
            CantorNode node = getCantorNode(list.get(i));
            
// 输出结果
            if (node != null{
                System.out.println(node.toString());
            }

        }

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值