当前位置: 首页 > 知识库问答 >
问题:

使用指定的操作将一个数字转换为另一个数字。在这方面我需要一些帮助

左丘凡
2023-03-14

这是我最近遇到的问题-

null

假设我有三个整数k,m,n。我必须以最少的操作次数从k到达m,可能的操作是-

你可以把k乘以n。

你可以把k降低2。

你可以把k减1。

此外,这3个操作可以按任何顺序执行。

有很多方法可以尝试--无论是递归还是动态方法。但是我找到了一个有趣的解决方案,它没有实现这两个,而且我很难理解它。这里有代码供参考-

int k = sc.nextInt();
int m = sc.nextInt();
int n = sc.nextInt();
int count=0;
int x=0;
while(k<m)
{
if(m%n==0){
m=m/n;
count++;
}
else{
x=(n-(m%n));
m+=(x)/2*2+(x)%2;
count+=x/2+x%2;
}
}
if(k>m)
{
count+=(k-m)/2+(k-m)%2;
}
System.out.println(count);
}

好吧,我真的很抱歉不能包含注释,因为我不能得到这段代码的诀窍。谁能请您把代码读一遍,并解释一下这段代码实际上是如何工作的吗?那会有很大的帮助!(顺便说一下,代码运行良好!)

共有2个答案

干永丰
2023-03-14

我想我已经简化了代码

int k = 2;
int m = 5;
int n = 6;
int count = 0;
while (k < m) {
    k *=n;
    count++;
}
if (k > m) {
    count += (k - m) / 2 + (k - m) % 2;
}
System.out.println(count);

请让我知道它失败的用例。输出:

5
黄宏毅
2023-03-14

下面是一个注释版本(重新格式化也有很大帮助):

    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int m = sc.nextInt();
    int n = sc.nextInt();
    
    sc.close();

    int count = 0, x;
    while (k < m) { // Loop while k is smaller then m (meaning our end goal for m is to be smaller or equal to k) (since k will not change inside of the while loop we know that m must change)
        if (m % n == 0) { // Check if m divided by n has NO remainder, if so do the division and increase count by one
            m = m / n;
            count++;
        } else { // If not then...
            // ...subtract the remainder from n and store it in x. X now contains the number which you would need to add to m to make 'm % n == 0' (the if statement) result in true
            x = (n - (m % n));

            // In here we do 'x / 2 * 2' first which will result in even value (rounding x to its lower even value. eg. x = 5 would become 4. Since we are using integers) which we then add to m.
            // Then we also add the missing '1' to m to make the if statement in the next loop result in true
            // I tested this algorithm btw. it does not change the value of x:
            /*
                for (int x = 0; x < 5000; x++) {
                    boolean same = x == (x / 2 * 2 + x % 2);
                    System.out.println(x + " -> " + same);
                }
                Result: same is always true
            */
            m += x / 2 * 2 + x % 2; // This line can be simplified to 'm += x;'
            
            // Then we add half of the x and a 0 or a 1 to count
            count += x / 2 + x % 2;
        }
    }
    if (k > m) { // This will run always if k is not eual to m
        
        // This line is almost equal to this one: 'count += x / 2 + x % 2;'
        // except, that every x was replaced by '(k - m)'
        
        count += (k - m) / 2 + (k - m) % 2;
    }
    
    // Then we print out count
    System.out.println(count);
 类似资料:
  • 有一些东西让我困惑,我没有找到关于VM规范的太多信息。这有点晦涩,如果有人能给我解释一下就好了。 这几行代码...... ..... 生成此输出: 浮动:无限 int: 2147483647 短:-1 字节:-1 、和是8位、16位和32位,带有两个补码和是32位和64位的IEEE 754(参见此处)。 根据我的理解,最大值的意味着尾数的所有位(52位)都切换到1。因此,转换为短或字节返回-1,即

  • 我正在研究将字符串从一个字符集转换为另一个字符集,阅读了很多关于它的示例,最终找到了下面的代码,这对我来说很好,作为一个字符集编码的新手,我想知道,这是否是正确的方法。 要将字符串从ASCII转换为EBCDIC,我必须执行以下操作: 要将EBCDIC转换为ASCII,我必须:

  • 问题内容: 我的朋友正在尝试将某种计算作为上课的一项任务,但他遇到了一些麻烦…希望您能为他提供帮助。 问题在于他从用户那里得到了一个int的输入(这必须是任务的一部分)。他正在尝试在下面的代码中将其转换为double,但这是行不通的。无论如何结果都是int。 如果您需要更多说明,我会请他提供。提前致谢! 问题答案: 您必须将一个(或两个)参数传递给除法运算符: 由于您两次执行相同的计算,因此建议您

  • 我需要使用JOLT转换将下面的输入转换为精确的输出。输入: 输出: 我尝试使用以下规格,但没有驱动到我需要的精确输出。规格: 任何帮助都将不胜感激。谢谢

  • 我有下面的JSON输入,我想删除值标签并在TypeOfSTA中设置标题值。 我想使用JOLT转换在输出JSON中进行转换。 下面是我的请求JSON和输出JSON。 请求 JSON: 请帮助我使用JOLT转换进行转换。 输出JSON:

  • 假设我有以下数组列表: 并且必须遵守规则: 从数组列表 1 开始,我想形成新的以下数组列表: <李>猫狗 <李>鼠蛇 无论如何都可以这样做。我目前还没有找到任何字符串到字符串转换的内容。