繁体   English   中英

如何在不使用控制语句或循环的情况下将整数中的所有数字(在000和1000之间)相乘?

[英]How can I multiply all the digits in an integer (between 000 & 1000 exclusive) without using control statements or loops?

我正在尝试编写一个程序,它可以将数字的所有数字从0添加到1000,仅使用Java中的数学表达式。 只要用户输入一个3位数字,我的程序就可以正常工作,但如果输入的数字少于100,则结果为0。

我已经尝试用'%10'获取输入的最后一位数并用'/ 10'删除最后一位但没有控制语句来检测输入是否已减少到零,程序最终乘以0乘以0 2位数字已减少为零,结果不正确。

public class MultiplyDigits {
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000: ");
        int number = input.nextInt();
        int product = 1;
        product*=number%10;
        number/=10;
        product*=number%10;
        number/=10;
        product*=number%10;
        System.out.println(product);
    }
}

输入55应该导致25,但我的程序执行5 x 5 x 0 = 0

输入999会产生729,这是正确的。 9 x 9 x 9 = 729

更进一步的澄清,这是完整新手教科书第2章中的一个问题。 作者没有涵盖选择语句,循环,编写我们自己的方法或类,或者比基本编程更先进的东西,所以暗示如果没有这些,这是可行的。 本书已经介绍了在Java中构建的类中的调用方法,尽管作者只提到了Math和System类中的方法。 例如,Math.max(),Math.min(),Math.pow(),System.currentTimeMillis();

这个变种怎么样? 要查找第一个数字,首先可以减少输入的数字100,并在乘法时加1以避免0。 并且,作为推荐的NVioli,第二个数字应该相同更新,以便有可能输入低于10的数字。因此,最终的变体是:

int number = input.nextInt();

    int t1 = 1 + (number-100) / 100;
    int t2 = (1 + (number-10) / 10) % 10; \\By NVioli
    int t3 = number % 10;

    int  product = t1 * t2 * t3;

    System.out.println(product);

第一部分是将基本代码提取到单独的Java方法中。 我称之为dprod ,这是“数字产品”的缩写。

static int dprod(int x) {
    int hun = x / 100 % 10;
    int ten = x / 10 % 10;
    int one = x / 1 % 10;

    return hun * ten * one;
}

上面的代码是天真的版本,仅适用于>= 100数字。

要按预期处理小于100的数字,如果为0,则需要将hunten替换为1。

static int dprod(int x) {
    int hun = x < 100 ? 1 : x / 100 % 10;
    int ten = x < 10 ? 1 : x / 10 % 10;
    int one = x / 1 % 10;

    return hun * ten * one;
}

?:运算符称为条件运算符,因此根据您的规则可能不允许。 通过使用Math.max函数,可以使用?:运算符而无需显式编写它。

static int dprod(int x) {
    int hun = Math.max(100, x) / 100 % 10;
    int ten = Math.max(10, x) / 10 % 10;
    int one = x / 1 % 10;

    return hun * ten * one;
}

Math.max函数在内部使用?:运算符,因此也可能被禁止。 这可以讨论,因为它取决于规则的确切措辞及其意图。

如果Math.max是禁止的,所以能够完全实现它没有分支或条件,参见该C ++问题 ,其可以通过更换被翻译成Java int32int和通过更换inlinestatic

完整的代码,包括自动测试,是:

package de.roland_illig.so;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class DprodTest {

    static int dprod(int x) {
        int hun = Math.max(x, 100) / 100 % 10;
        int ten = Math.max(x, 10) / 10 % 10;
        int one = x / 1 % 10;

        return hun * ten * one;
    }

    @Test
    public void testDprod() {
        assertThat(dprod(999)).isEqualTo(729);
        assertThat(dprod(123)).isEqualTo(6);
        assertThat(dprod(99)).isEqualTo(81);
        assertThat(dprod(9)).isEqualTo(9);
    }
}

您可以使用长度为1000的数组初始化程序,使用每个数字的值初始化它,然后您的真正问题简化为:

System.out.println(calculatedArray[number]);

您的初始化甚至可以利用前导0根据您的规则无关紧要的事实(55和155是相同的结果。)

calculatedArray[55] = calculcate(155);

有一些方法可以帮助你,但所有这些方法都有一个简单的循环或如果:

  1. 您可以使用digits = Logarithm of your number(10 base)然后您有数字位数,然后您可以使用循环来计算结果。 你的循环将重复digit时间,所以无论你的数字有多少位数,它都会一直有效。

  2. 您可以检查您的数字是否小于100,然后只需添加100 ,然后计算结果,因为1 * digit1 * digit2将没有错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM