我给了数字,并希望它以扩展形式的String返回。例如
expandedForm(12); # Should return "10 + 2"
expandedForm(42); # Should return "40 + 2"
expandedForm(70304); # Should return "70000 + 300 + 4"
我的函数适用于第一种情况和第二种情况,但是使用70304可以达到以下目的:
70 + 00 + 300 + 000 + 4
这是我的代码
import java.util.Arrays;
public static String expandedForm(int num)
{
String[] str = Integer.toString(num).split("");
String result = "";
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[j] += '0';
}
}
}
result = Arrays.toString(str);
result = result.substring(1, result.length()-1).replace(",", " +");
System.out.println(result);
return result;
}
我认为第二个循环有问题,但不知道为什么。
答案 0 :(得分:14)
您应该在str[i]
上添加0,而不是str[j]
:
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[i] += '0';
}
}
}
这将导致:
70000 + 0 + 300 + 0 + 4
您仍然必须摆脱0位数。
摆脱它们的一种可能方法:
result = result.substring(1, result.length()-1).replace(", 0","").replace(",", " +");
现在的输出是
70000 + 300 + 4
答案 1 :(得分:6)
伪代码使用整数算术(从右边的一位开始)一对一地提取十进制数字:
mul = 1 //will contain power of 10
while (num > 0):
dig = num % 10 //integer modulo retrieves the last digit
if (dig > 0): //filter out zero summands
add (dig * mul) to output //like 3 * 100 = 300
num = num / 10 //integer division removes the last decimal digit 6519 => 651
mul = mul * 10 //updates power of 10 for the next digit
答案 2 :(得分:3)
您可以使用模%
和整数除法/
对纯数学进行相同的处理,例如使用Stream
API:
int n = 70304;
String res = IntStream
.iterate(1, k -> n / k > 0, k -> k * 10) // divisors
.map(k -> (n % (k*10) / k ) * k) // get 1s, 10s, 100s, etc.
.filter(x -> x > 0) // throw out zeros
.mapToObj(Integer::toString) // convert to string
.collect(Collectors.joining(" + ")); // join with '+'
System.out.println(res); // 4 + 300 + 70000
答案 3 :(得分:2)
可能有很多变化。如果允许使用列表:
public static String expandedForm(int num){
String[] str = Integer.toString(num).split("");
String result;
List<String> l = new ArrayList<String>();
for(int i = 0; i < str.length; i++){
if(Integer.valueOf(str[i]) > 0){
String s = str[i];
for(int j = i; j < str.length - 1; j++){
s += '0';
}
l.add(s);
}
}
result = l.toString();
result = result.substring(1, result.length() - 1).replace(",", " +");
System.out.println(result);
return result;
}
一个人也可以直接处理结果:
public static String expandedForm2(int num){
String[] str = Integer.toString(num).split("");
String result = "";
for(int i = 0; i < str.length; i++){
if(Integer.valueOf(str[i]) > 0){
result += str[i];
for(int j = i; j < str.length - 1; j++){
result += '0';
}
result += " + ";
}
}
result = result.substring(0, result.length() - 3);
System.out.println(result);
return result;
}
答案 4 :(得分:1)
这也可以递归进行。这里是一个示例实现:
String g(int n, int depth){ // Recursive method with 2 int parameters & String return-type
int remainder = n % depth; // The current recursive remainder
if(depth < n){ // If we aren't done with the number yet:
int nextDepth = depth * 10; // Go to the next depth (of the power of 10)
int nextN = n - remainder; // Remove the remainder from the input `n`
// Do a recursive call with these next `n` and `depth`
String resultRecursiveCall = g(nextN, nextDepth);
if(remainder != 0){ // If the remainder was not 0:
// Append a " + " and this remainder to the result
resultRecursiveCall += " + " + remainder;
}
return resultRecursiveCall; // And return the result
} else{ // Else:
return Integer.toString(n); // Simply return input `n` as result
}
}
String f(int n){ // Second method so we can accept just integer `n`
return g(n, 1); // Which will call the recursive call with parameters `n` and 1
}
第二种方法是这样,我们可以只用一个输入n
来调用该方法。例如:
String result = f(70304);
这将导致字符串70000 + 300 + 4
。
要进一步解释此递归方法的作用,让我们对输入70304
进行逐步操作:
n=70304
,depth=1
,remainder=70304%1 = 0
。
depth < n
是真实的,它将使用70304-0
和1*10
进行递归调用remainder
为0,它将不再附加任何结果n=70304
,depth=10
,remainder=70304%10 = 4
。
depth < n
仍然是真实的,它将使用70304-4
和10*10
进行递归调用remainder
为4,它将在结果后附加一个" + "
和这个4
n=70300
,depth=100
,remainder=70300%100 = 0
。
depth < n
仍然是真实的,它将使用70300-0
和100*10
进行递归调用remainder
为0,它将不再附加任何结果n=70300
,depth=1000
,remainder=70300%1000 = 300
。
depth < n
仍然是真实的,它将使用70300-300
和1000*10
进行递归调用remainder
是300,它将在结果后附加一个" + "
和这个300
n=70000
,depth=10000
,remainder=70000%10000 = 0
。
depth < n
仍然是真实的,它将使用70000-0
和10000*10
进行递归调用remainder
为0,它将不再附加任何结果n=70000
,depth=100000
,remainder=70000%100000 = 70000
。
depth < n
是虚假的,因此它将不再执行任何递归调用,而是返回当前的n
(即70000
)。由于这些都是递归调用,因此我们实际上应该向后看结果,因此它将得到70000 + 300 + 4
。
所以通常:
depth < n
if-check用于查看何时完成递归调用。g(n-remainder, depth*10)
将删除我们在上一个递归迭代中已经输出的数字,并在下一个递归迭代中使用下一个10 k
次幂 li>
remainder != 0
if-check确定我们要附加的号码是否不是0
答案 5 :(得分:1)
public class Kata
{
public static String expandedForm(int num)
{
String outs = "";
for (int i = 10; i < num; i *= 10) {
int rem = num % i;
outs = (rem > 0) ? " + " + rem + outs : outs;
num -= rem;
}
outs = num + outs;
return outs;
}
}
答案 6 :(得分:0)
package backup;
import java.util.Arrays;
public class FileOutput {
public static void main(String[] args){
String expForm = expandedForm(70304);
//System.out.println(expForm);
}
public static String expandedForm(int num)
{
String[] str = Integer.toString(num).split("");
String result = "";
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[i] += '0';
}
}
}
result = Arrays.toString(str);
result = result.substring(1, result.length()-1).replace(",", " +");
System.out.println(result);
return result;
}
}
输出:70000 + 0 + 300 + 0 + 4
大多数内部循环中的解决方案都需要在str[i]
中添加'0':str[i] += '0';
然后,您需要从结果输出中替换“ + 0”。
答案 7 :(得分:0)
for(int i = 0; i < str.length; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = 0; j < str.length - i - 1; j++) {
str[i] += '0';
}
}
}
答案 8 :(得分:0)
我认为这个问题的挑战是在遍历整个数字时省略0(零)和多余的+(加号)。字符串concat函数可以在以下条件下使用:
public static String expandedForm(int num) {
String st = String.valueOf(num);
String finalResult = "";
for (int i = 0; i < st.length(); i++) {
String s = String.valueOf(st.charAt(i));
if (Integer.valueOf(s) > 0) {
for (int j = i; j < st.length() - 1; j++) {
s = s.concat("0");
}
if (i == st.length() - 1) {
finalResult = finalResult.concat(s);
} else {
finalResult = finalResult.concat(s + " + ");
}
}
}
return finalResult;
}
答案 9 :(得分:0)
public static String expandedForm(int num)
{
String[] str = Integer.toString(num).split("");
String result = "";
String st="";
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[i] += '0';
}
}
}
for(String s:str) {
st += s+" ";
}
result=st;
result = result.substring(0, result.length()-1).replace(" 0","").replace(" ", " + ");
System.out.println(result);
return result;
}
答案 10 :(得分:0)
public static String expandedForm(int num)
{
int numberOfDigits =(int) Math.floor(Math.log10(num)+1);
String result="";
while(numberOfDigits-->0){
int divisor = (int)Math.pow(10,numberOfDigits);
int quotient = num/divisor;
num%=divisor;
int value = quotient * divisor;
if(value!=0)
result+=value+(numberOfDigits != 0 ?"+":"");
}
return result;
}
尝试一下。
查找位数并为每个位数进行迭代。
除以10的除数(数字为-1),并将该数字除以得到商。
提醒下一次迭代。
将商和除数相乘并存储结果(如果值不为零)。