- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在处理 exercism.io 的一个问题,该问题要求函数 s.t.给定一个数字数组、一个指定的输入基数(例如基数 2)和一个输出基数(例如基数 10),该函数的输出将是新基数中相同数字的数字数组。
例如如果输入是 Rebase(2, { 1, 0, 1, 0, 1, 0 }, 10)
输出应该是 {4, 2}
我已经尝试实现这个问题的解决方案,首先将数字重新设置为基数 10,然后根据 this page 上的方法对数字进行重新设置。取余数和除以基数的商来找到数字的位数。
using System;
using System.Collections.Generic;
using System.Linq;
public static class AllYourBase
{
public static int[] Rebase(int inputBase, int[] inputDigits, int outputBase)
{
int baseTen = 0;
int remainder;
int quotient;
LinkedList<int> coeffHolder = new LinkedList<int>();
if (outputBase <= 1)
throw new ArgumentException();
if(inputBase <= 1)
throw new ArgumentException();
for(int i = inputDigits.Length-1; i >= 0; i--)
{
baseTen += (int)inputDigits[i] * (int)Math.Pow(inputBase, i);
}
quotient = baseTen;
while(quotient > 0)
{
remainder = quotient % outputBase;
coeffHolder.AddFirst(remainder);
quotient = (int)quotient / (int)outputBase;
}
return coeffHolder.ToArray();
}
}
如上所述,我对 Rebase(2, { 1, 0, 1, 0, 1, 0 }, 10)
的预期输出是 {4, 2}
,但出于某种原因,我得到的答案是 {2, 1}
。
我不确定为什么我得到的结果只有我预期的一半。
附言我知道有内置方法可以更改基数,但由于这是一个练习,我更倾向于通过我自己的函数实现来转换它。
如有任何帮助,我们将不胜感激!
最佳答案
您正在以相反的顺序(从右到左)处理数字,并在处理过程中(从右到左)减少 2(输入基数)的指数。
这会以 2 的降幂(inputBase)从左到右处理数字。
for (int i = 0; i < inputDigits.Length; i++)
{
int j = inputDigits.Length - (i + 1);
baseTen += (int)inputDigits[i] * (int)Math.Pow(inputBase, j);
}
关于在任意两个数字基数之间转换的 C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56841242/
我想要以下内容: void foo( /* something representing a function f */, /* arguments a1, a2, etc. in s
简而言之,我想声明一个这样的特征: trait Test { def test(amount: Int): A[Int] // where A must be a Monad } 这样我就可以
在 GWT 中,如何在 onModuleLoad 方法中插入框架集以及相对嵌套的框架集和框架,以合并许多小程序和其他小部件和 HTML?代码片段是: 公共(public)类 MainEntryPoin
这个问题在这里已经有了答案: How do I best simulate an arbitrary univariate random variate using its probability
我对java相当陌生,并且习惯于枚举本质上只不过是一个命名的整数列表。 现在我正在编写一个实现,其中父类有几个采用枚举值作为参数的方法。枚举将在子类中定义,并且会略有不同。由于枚举基本上看起来像类,所
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
想象一下 6-7 台服务器的设置都完全相同Java 版本“1.6.0_18”OpenJDK 运行时环境 (IcedTea6 1.8) (fedora-36.b18.fc11-i386)OpenJDK
这个问题在这里已经有了答案: What are some uses of template template parameters? (10 个答案) 关闭 4 年前。 我有一个根据策略舍入值的函数
我正在寻找如何在 Java 中给定一个 Async CompletableFutures 列表,以便前 N 个中的任何一个成功完成或失败。除非没有 N 次成功,否则忽略任何失败。 有这方面的例子吗?
我面临的问题是项目已经使用集群编程来分配任务。 if (cluster.isMaster) { // Fork workers. for (var i = 0; i { }); } el
我正在为 Luxology modo(3D 和 VFX 应用程序)编写脚本,该脚本使用 python 作为脚本语言。在我的脚本中的某个位置,我正在读取从其他应用程序输出的文本文件,并从该文本文件的行创
这个问题在这里已经有了答案: Fast arbitrary distribution random sampling (inverse transform sampling) (5 个答案) 关闭
我只是遇到了一个问题,我有一个结构数组,例如 package main import "log" type Planet struct { Name string `json:"
我正在尝试将 class ResponseResult 编码为 json case class ResponseResult (var Code : Int, var
我想将一个矩阵中的一个 block 复制到另一个矩阵的一部分中。要将其与任何类型的 n 维数组一起使用,我需要通过 [] 运算符应用带有偏移量的列表。有办法做到这一点吗? mat_bigger[0:5
我有一个匹配一组数字和字母的正则表达式。但是我希望能够排除任何三个连续的字母。这是为了防止意外形成单词或缩写。 我的表达如下。它还排除了一些类似的字符,如 0、o、O 和 1、i、I、l): ^[2-
根据documentation . 应匹配任何字符,但不匹配重音字符。 mysql> select 'test' regexp 't.st'; +----------------------+ | '
我该如何用 JavaScript 编写这个 if 语句? if(url == "http://www.google.com/" && "*") { ... } * 需要灵活并接受添加到第一个变量上
我知道 cPython 有一个 GIL,因此如果不使用多处理模块,您的脚本就无法在多个内核上运行。但是有什么可以阻止内置功能,例如使用多核进行排序吗?我不了解 cPython 结构,但我想我要问的问题
寻找命令行 gdb 的替代方法来检查 OSX 上的核心转储 - 有没有办法让 Xcode 打开带有调试符号的任意核心转储? 最佳答案 您是否尝试过使用 MachOView 1? 听起来它可能适用于查看
我是一名优秀的程序员,十分优秀!