1281:整数的各位积和之差

This commit is contained in:
轩辕龙儿 2022-04-13 10:18:31 +08:00
parent 58348829a0
commit 2d9ac9515f
2 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,61 @@
//给你一个整数 n请你帮忙计算并返回该整数各位数字之积各位数字之和的差
//
//
//
// 示例 1
//
// 输入n = 234
//输出15
//解释
//各位数之积 = 2 * 3 * 4 = 24
//各位数之和 = 2 + 3 + 4 = 9
//结果 = 24 - 9 = 15
//
//
// 示例 2
//
// 输入n = 4421
//输出21
//解释
//各位数之积 = 4 * 4 * 2 * 1 = 32
//各位数之和 = 4 + 4 + 2 + 1 = 11
//结果 = 32 - 11 = 21
//
//
//
//
// 提示
//
//
// 1 <= n <= 10^5
//
// Related Topics 数学 👍 81 👎 0
package leetcode.editor.cn;
//1281:整数的各位积和之差
public class SubtractTheProductAndSumOfDigitsOfAnInteger {
public static void main(String[] args) {
Solution solution = new SubtractTheProductAndSumOfDigitsOfAnInteger().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int subtractProductAndSum(int n) {
int sum = 0;
int mul = 1;
while (n >= 10) {
int tmp = n % 10;
sum += tmp;
mul *= tmp;
n /= 10;
}
sum += n;
mul *= n;
return mul - sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,32 @@
<p>给你一个整数&nbsp;<code>n</code>,请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>n = 234
<strong>输出:</strong>15
<strong>解释:</strong>
各位数之积 = 2 * 3 * 4 = 24
各位数之和 = 2 + 3 + 4 = 9
结果 = 24 - 9 = 15
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>n = 4421
<strong>输出:</strong>21
<strong>解释:
</strong>各位数之积 = 4 * 4 * 2 * 1 = 32
各位数之和 = 4 + 4 + 2 + 1 = 11
结果 = 32 - 11 = 21
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10^5</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li></div></div><br><div><li>👍 81</li><li>👎 0</li></div>