5738:K 进制表示下的各位数字总和

This commit is contained in:
huangge1199 2021-04-25 13:14:22 +08:00
parent c68b16ff80
commit 24fe5d7199
3 changed files with 91 additions and 3 deletions

View File

@ -0,0 +1,59 @@
//给你一个整数 n10 进制和一个基数 k 请你将 n 10 进制表示转换为 k 进制表示计算并返回转换后各位数字的 总和
//
// 转换后各位数字应当视作是 10 进制数字且它们的总和也应当按 10 进制表示返回
//
//
//
// 示例 1
//
//
//输入n = 34, k = 6
//输出9
//解释34 (10 进制) 6 进制下表示为 54 5 + 4 = 9
//
//
// 示例 2
//
//
//输入n = 10, k = 10
//输出1
//解释n 本身就是 10 进制 1 + 0 = 1
//
//
//
//
// 提示
//
//
// 1 <= n <= 100
// 2 <= k <= 10
//
// Related Topics 位运算 数学
// 👍 0 👎 0
package leetcode.editor.cn;
//5738:K 进制表示下的各位数字总和
public class SumOfDigitsInBaseK{
public static void main(String[] args) {
//测试代码
Solution solution = new SumOfDigitsInBaseK().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int sumBase(int n, int k) {
int sum = 0;
String str = "";
while (n > 0) {
str += n % k;
n /= k;
}
for (int i = 0; i < str.length(); i++) {
sum += str.charAt(i) - '0';
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,31 @@
<p>给你一个整数 <code>n</code><code>10</code> 进制)和一个基数 <code>k</code> ,请你将 <code>n</code><code>10</code> 进制表示转换为 <code>k</code> 进制表示,计算并返回转换后各位数字的 <strong>总和</strong></p>
<p>转换后,各位数字应当视作是 <code>10</code> 进制数字,且它们的总和也应当按 <code>10</code> 进制表示返回。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 34, k = 6
<strong>输出:</strong>9
<strong>解释:</strong>34 (10 进制) 在 6 进制下表示为 54 。5 + 4 = 9 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 10, k = 10
<strong>输出:</strong>1
<strong>解释:</strong>n 本身就是 10 进制。 1 + 0 = 1 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>2 <= k <= 10</code></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>数学</li></div></div>\n<div><li>👍 0</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long