- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class ProductOfDigits {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = input.nextInt();
int x = Math.abs(n);
int product = 1;
if (x >=0 && x<=9) {
System.out.println(x);
}
else {
product = product * (x % 10);
x = x/10;
System.out.println(product);
}
当输入为负数时,乘积是第一个数字和最后一个数字之间的结果。谁能解释一下吗?我尝试过使用 Math.abs() 来获取绝对值,但这是不可能的,而且现在简直要了我的命。
最佳答案
按如下方式进行:
import java.util.Scanner;
public class ProductOfDigits {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = input.nextInt();
int x = Math.abs(n);
int ç = 1;
int product = 1;
if (x == 0) {
product = 0;
} else {
while (x > 0) {
product *= x % 10;
x /= 10;
}
}
System.out.println(product);
}
}
示例运行 1:
Enter an integer: -256
60
示例运行 2:
Enter an integer: 0
0
示例运行 3:
Enter an integer: 9
9
示例运行 4:
Enter an integer: 256
60
递归版本:
import java.util.Scanner;
public class ProductOfDigits {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = input.nextInt();
System.out.println(productOfDigits(n));
}
static int productOfDigits(int n) {
n = Math.abs(n);
if (n > 0 && n < 10) {
return n;
}
if (n == 0) {
return 0;
}
return (n % 10) * productOfDigits(n / 10);
}
}
关于java - 数字的乘积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60129476/
我希望我的问题有一个非常简单的解决方案。我只是找不到它: 假设您有两个向量(一个是列向量,一个是行向量)A、B: A = [1,2,3] B = [4;5;6] 如果我们按如下方式将它们相乘,我们会得
我有一个 Tuple 的列表: "dog", 25 "cat", 5 "cat", 7 "rat", 4 "dog", 10 我需要的 Linq 查询规则必须满足以下条件:我需要按字符串值对元组进行分
给定 2 个不同的 NDarray,A 和 B,形状相同但尺寸任意,我如何获得 NDarray C,其中 C 是 A 和 B(含)范围内所有整数的乘积。 我的意思是A是起始数组,B是结束数组,我想要数
假设我需要为某些输入构建一个真值表,它要求我提供逻辑和、算术和和逻辑乘积。它们之间有什么区别? 最佳答案 逻辑和 - 一种计算机加法,当一个或两个输入变量为 1 时,结果为 1;当输入变量均为 0 时
我正在尝试执行一个简单的矩阵乘法 vector 乘法,但出于某种原因,我在几次乘法的结果中得到了错误的符号。我不知道为什么会这样,任何指针将不胜感激。 这是我的全部代码,即矩阵 * vector 函数
我在上一个主题中找到了一些关于 cuda 矩阵 vector 积的代码: Matrix-vector multiplication in CUDA: benchmarking & performanc
我遇到的第一个问题是显示三个数字中的最小和最大。出现两个单独的警报 - 第一个警报说第二大数字是最大的(因为它还没有考虑第三个数字),第二个警报正确地指出三个中最大的数字是最大的.不确定为什么会这样—
我有两个矩阵 a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) 我想得到元素乘积,[[1*5,2*6], [3*7,4*8]],等
我有一个数组和一个 vector : ArrayXd m1(3, 1337); ArrayXd v1(1, 1337); ArrayXd result(3, 1337); 现在我想将 m1 的每一行与
我有两个 3D 矩阵: a = np.random.normal(size=[3,2,5]) b = np.random.normal(size=[5,2,3]) 我想要每个切片分别沿 2 轴和 0
我正在创建一个 C++ 软件,我需要一个包装器,它基于 Eigen 库,实现类似于官方网页中解释的运算符* https://eigen.tuxfamily.org/dox/group__Matrixf
我正在尝试将张量 (m, n, o) 分解为矩阵 A(m, r)、B (n, r) 和 C (k, r)。这被称为 PARAFAC 分解。 Tensorly已经做了这种分解。 一个重要的步骤是将 A、
我目前正面临这个问题。我有两个矩阵 MatrixXf答: 0.5 0.5 0.5 0.50.694496 0.548501 0.680067 0.7171110
我有以下 df: df = pd.DataFrame({'A': ['foo', 'bar', 'dex', 'tru'], 'B': ['abc', 'def'
假设我们有 2 个 2X2 numpy 数组: X=np.array([[0,1],[1,0]]) 和 I=np.array([[1,0],[0,1]]) 考虑一下克罗内克产品 XX=X^X 我让符号
我想弄清楚这是 Eigen 中的错误还是我做错了什么。我只想要两个复数 vector [1,i] 和 [1,-i] 的点积。答案是 1*1 + i*(-i) = 2。但是 Eigen 给出的答案是零。
我的 C 代码有问题。我所做的就是这样: #include int main() { float zahlen[2]; for (int i = 0; i < 2; i++) {
为了找到数字的因数,我正在使用函数 void primeFactors(int n) # include # include # include # include using namespa
我是一名优秀的程序员,十分优秀!