| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 696 人关注过本帖
标题:数据进制转换的笔试题
只看楼主 加入收藏
西鄙人
Rank: 2
等 级:论坛游民
帖 子:36
专家分:12
注 册:2009-12-2
结帖率:100%
收藏
 问题点数:0 回复次数:1 
数据进制转换的笔试题
/**
 *
 * 一个7进制数字,把它转换为10进制。尽量运行快,内存占用少。不能用幂运算、不能用Math类
 * 抛砖引玉
 */
package test;
import java.util.Date;
public class NumberCase {

        
    private static int convert(int num){
        int total=0;
        int weithValue = 1;
        do{
            total = total+(weithValue)*(num%10);
            weithValue = (weithValue<<3)-weithValue;
            num = num/10;
        }while(num>0);
        return total;
    }
   
    public static int convertUseMath(int num){
        int total=0;
        for(int i=0;num>0;i++){
            //System.out.println(Math.pow(7,i));
            total = total +(int)((num%10)*Math.pow(7,i));
            num = num/10;
        }
        
        return total;
    }
   
    public static void main(String[] args) {
        int result = 0;
        long start = new Date().getTime();
        for(int i=0;i<1000000;i++){
            result = convert(1234568765);
            //result = convertUseMath(1234568765);
        }
        long end = new Date().getTime();
        System.out.println(result+">>>>>"+(end-start));
    }

}

[ 本帖最后由 西鄙人 于 2011-7-11 22:33 编辑 ]
搜索更多相关主题的帖子: public 
2011-07-11 22:28
makebest
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:658
专家分:962
注 册:2005-3-17
收藏
得分:0 
很简单,初值n=0,然后从最高位算到最低位,如下操作
n*=7; n+={那一位的数值}
2011-07-11 23:11
快速回复:数据进制转换的笔试题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014204 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved