将小数转换为混合基数(基数)

发布于 2024-07-17 09:33:33 字数 142 浏览 7 评论 0原文

如何将十进制数转换为混合基数表示法?

我猜想,给定每个基数的数组和十进制数的输入,它应该输出每列值的数组。

How do you convert a decimal number to mixed radix notation?

I guess that given an input of an array of each of the bases, and the decimal number, it should output an array of the values of each column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见 2024-07-24 09:33:33

伪代码:

bases = [24, 60, 60]
input = 86462                       #One day, 1 minute, 2 seconds
output = []

for base in reverse(bases)
    output.prepend(input mod base)
    input = input div base          #div is integer division (round down)

Pseudocode:

bases = [24, 60, 60]
input = 86462                       #One day, 1 minute, 2 seconds
output = []

for base in reverse(bases)
    output.prepend(input mod base)
    input = input div base          #div is integer division (round down)
木有鱼丸 2024-07-24 09:33:33

数量-> 设置:

factors = [52,7,24,60,60,1000]
value = 662321
for i in n-1..0
  res[i] = value mod factors[i]
  value = value div factors[i]

反之亦然:

(1000) 等数字,并且希望将其转换为十进制:

如果您有 32(52)、5(7)、7(24)、45(60)、15(60)、 500 数字n,乘以n-1的因子,继续n-1..n=0

values = [32,5,7,45,15,500]
factors = [52,7,24,60,60,1000]

res = 0;
for i in 0..n-1
  res = res * factors[i] + values[i]

,你就得到了这个数字。

Number -> set:

factors = [52,7,24,60,60,1000]
value = 662321
for i in n-1..0
  res[i] = value mod factors[i]
  value = value div factors[i]

And the reverse:

If you have the number like 32(52), 5(7), 7(24), 45(60), 15(60), 500(1000) and you want this converted to decimal:

Take number n, multiply it with the factor of n-1, continue for n-1..n=0

values = [32,5,7,45,15,500]
factors = [52,7,24,60,60,1000]

res = 0;
for i in 0..n-1
  res = res * factors[i] + values[i]

And you have the number.

又怨 2024-07-24 09:33:33

Java中,你可以做

public static int[] Number2MixedRadix(int[] base, int number) throws Exception {
            //NB if the max number you want @ a position is say 3 then the base@ tha position
            //in your base array should be 4 not 3

            int[] RadixFigures = new int[base.length];
            int[] PositionPowers = new int[base.length];
            PositionPowers[base.length-1] = 1;
            for (int k = base.length-2,pow = 1; k >-1; k--){
                pow*=base[k+1];
                PositionPowers[k]=pow;
            }for (int k = 0; k<base.length; k++){
                RadixFigures[k]=number/PositionPowers[k];
                if(RadixFigures[k]>base[k])throw new Exception("RadixFigure@["+k+"] => ("+RadixFigures[k]+") is > base@["+k+"] => ("+base[k]+") | ( number is Illegal )");
                number=number%PositionPowers[k];
            }return RadixFigures;
        }

示例

//e.g. mixed-radix base for 1day
int[] base = new int[]{1, 24, 60, 60};//max-day,max-hours,max-minutes,max-seconds
int[] MixedRadix = Number2MixedRadix(base, 19263);//19263 seconds
//this would give [0,5,21,3] => as per 0days 5hrs 21mins 3secs

反转

 public static int MixedRadix2Number(int[] RadixFigures,int[] base) throws Exception {
            if(RadixFigures.length!=base.length)throw new Exception("RadixFigures.length must be = base.length");
            int number=0;
            int[] PositionPowers = new int[base.length];
            PositionPowers[base.length-1] = 1;
            for (int k = base.length-2,pow = 1; k >-1; k--){
                pow*=base[k+1];
                PositionPowers[k]=pow;
            }for (int k = 0; k<base.length; k++){
                number+=(RadixFigures[k]*PositionPowers[k]);
                if(RadixFigures[k]>base[k])throw new Exception("RadixFigure@["+k+"] => ("+RadixFigures[k]+") is > base@["+k+"] => ("+base[k]+") | ( number is Illegal )");
            }return number;
        }

In Java you could do

public static int[] Number2MixedRadix(int[] base, int number) throws Exception {
            //NB if the max number you want @ a position is say 3 then the base@ tha position
            //in your base array should be 4 not 3

            int[] RadixFigures = new int[base.length];
            int[] PositionPowers = new int[base.length];
            PositionPowers[base.length-1] = 1;
            for (int k = base.length-2,pow = 1; k >-1; k--){
                pow*=base[k+1];
                PositionPowers[k]=pow;
            }for (int k = 0; k<base.length; k++){
                RadixFigures[k]=number/PositionPowers[k];
                if(RadixFigures[k]>base[k])throw new Exception("RadixFigure@["+k+"] => ("+RadixFigures[k]+") is > base@["+k+"] => ("+base[k]+") | ( number is Illegal )");
                number=number%PositionPowers[k];
            }return RadixFigures;
        }

Example

//e.g. mixed-radix base for 1day
int[] base = new int[]{1, 24, 60, 60};//max-day,max-hours,max-minutes,max-seconds
int[] MixedRadix = Number2MixedRadix(base, 19263);//19263 seconds
//this would give [0,5,21,3] => as per 0days 5hrs 21mins 3secs

Reversal

 public static int MixedRadix2Number(int[] RadixFigures,int[] base) throws Exception {
            if(RadixFigures.length!=base.length)throw new Exception("RadixFigures.length must be = base.length");
            int number=0;
            int[] PositionPowers = new int[base.length];
            PositionPowers[base.length-1] = 1;
            for (int k = base.length-2,pow = 1; k >-1; k--){
                pow*=base[k+1];
                PositionPowers[k]=pow;
            }for (int k = 0; k<base.length; k++){
                number+=(RadixFigures[k]*PositionPowers[k]);
                if(RadixFigures[k]>base[k])throw new Exception("RadixFigure@["+k+"] => ("+RadixFigures[k]+") is > base@["+k+"] => ("+base[k]+") | ( number is Illegal )");
            }return number;
        }
孤者何惧 2024-07-24 09:33:33

我想出了一个稍微不同的方法,可能不如这里的其他方法那么好,但我想无论如何我都会分享:

    var theNumber = 313732097; 
    
    //             ms   s   m   h    d
    var bases = [1000, 60, 60, 24, 365];
    var placeValues = [];  // initialise an array
    var currPlaceValue = 1;
    
    for (var i = 0, l = bases.length; i < l; ++i) {
        placeValues.push(currPlaceValue);
        currPlaceValue *= bases[i];
    }
    console.log(placeValues);
    // this isn't relevant for this specific problem, but might
    // be useful in related problems.
    var maxNumber = currPlaceValue - 1;
    
    
    var output = new Array(placeValues.length);
    
    for (var v = placeValues.length - 1; v >= 0; --v) {
        output[v] = Math.floor(theNumber / placeValues[v]);
        theNumber %= placeValues[v];
    }
    
    console.log(output);
    // [97, 52, 8, 15, 3] --> 3 days, 15 hours, 8 minutes, 52 seconds, 97 milliseconds

I came up with a slightly different, and probably not as good method as the other ones here, but I thought I'd share anyway:

    var theNumber = 313732097; 
    
    //             ms   s   m   h    d
    var bases = [1000, 60, 60, 24, 365];
    var placeValues = [];  // initialise an array
    var currPlaceValue = 1;
    
    for (var i = 0, l = bases.length; i < l; ++i) {
        placeValues.push(currPlaceValue);
        currPlaceValue *= bases[i];
    }
    console.log(placeValues);
    // this isn't relevant for this specific problem, but might
    // be useful in related problems.
    var maxNumber = currPlaceValue - 1;
    
    
    var output = new Array(placeValues.length);
    
    for (var v = placeValues.length - 1; v >= 0; --v) {
        output[v] = Math.floor(theNumber / placeValues[v]);
        theNumber %= placeValues[v];
    }
    
    console.log(output);
    // [97, 52, 8, 15, 3] --> 3 days, 15 hours, 8 minutes, 52 seconds, 97 milliseconds

紅太極 2024-07-24 09:33:33

我之前尝试了一些例子,发现了他们没有涵盖的边缘情况,如果你最大化了你的规模,你需要预先考虑最后一步的结果

def intToMix(number,radix=[10]):
    mixNum=[]

    radix.reverse()
    for i in range(0,len(radix)):
        mixNum.append(number%radix[i])
        number//=radix[i]
    mixNum.append(number)
    mixNum.reverse()
    radix.reverse()
    return mixNum


num=60*60*24*7

radix=[7,24,60,60]

tmp1=intToMix(num,radix)

I tried a few of the examples before and found an edge case they didn't cover, if you max out your scale you need to prepend the result from the last step

def intToMix(number,radix=[10]):
    mixNum=[]

    radix.reverse()
    for i in range(0,len(radix)):
        mixNum.append(number%radix[i])
        number//=radix[i]
    mixNum.append(number)
    mixNum.reverse()
    radix.reverse()
    return mixNum


num=60*60*24*7

radix=[7,24,60,60]

tmp1=intToMix(num,radix)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文