Java实践编程问题
我在HackerRank上解决这个问题 你可以在这里看到这个问题 https://www.hackerrank.com/contests/programming-jam-2-0/challenges/math-practice
You have devised a strategy that helps your kid practice his mathematics. First, you pick a positive integer N. Then you tell your kid to starts naming N, 3 × N, 5 × N, 7 x N and so on.
Whenever he names a number, he thinks about all of the digits in that number. He keeps track of which digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) he has seen at least once so far as part of any number he has named. Once he has seen each of the ten digits at least once, he is allowed to go play
还有我的代码,但当我提交时,它会给我(在标准输出上没有响应) 我修改了很多次代码,我不知道哪里出了错 有人能帮忙吗
package contest;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Mohamed
*/
public class Solution
{
public static void main(String [] s){
Scanner In = new Scanner(System.in);
int number = In.nextInt();
int counter = 1;
int length;
int oneDigit;
int newNumber;
int index;
boolean condition = true;
int [] arr = new int[10];
ArrayList<String> theList = new ArrayList<>();
while(condition = true){
length = (int)(Math.log10(number) + 1);
newNumber = number;
while(length > 0)
{
oneDigit = newNumber % 10;
newNumber = newNumber / 10;
if(arr[0] != oneDigit && arr[1] != oneDigit && arr[2] != oneDigit &&
arr[3] != oneDigit && arr[4] != oneDigit && arr[5] != oneDigit &&
arr[6] != oneDigit && arr[7] != oneDigit && arr[8] != oneDigit &&
arr[9] != oneDigit)
{
String num = String.valueOf(oneDigit);
theList.add(num);
index = theList.lastIndexOf(num);
arr[index] = oneDigit;
}
if(newNumber == 0){
length = 0;
}
else{
length = (int)(Math.log10(newNumber) + 1);
}
}
counter = counter + 2;
if(arr[9] != 0)
{
System.out.println(number);
condition = false;
}
number = number * counter;
}
}
}
# 1 楼答案
在本地调试!根据某些条件,它只有一个
println
。如果不满足此条件,您将无法打印任何内容此外,我建议进行一些重构,使代码更易于理解
将条件变量重命名为有意义的变量。比如isArray9Zero。为什么算法只在
arr[9] != 0
时停止用长i
f(arr[0] != oneDigit
生成命名函数。那里在检查什么# 2 楼答案
使用集合fid显示所看到的数字。集合不允许重复
中的代码应该如下所示: