有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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;
        }
      }
  }

共 (2) 个答案

  1. # 1 楼答案

    在本地调试!根据某些条件,它只有一个println。如果不满足此条件,您将无法打印任何内容

    此外,我建议进行一些重构,使代码更易于理解

    • 将条件变量重命名为有意义的变量。比如isArray9Zero。为什么算法只在arr[9] != 0时停止

    • 用长if(arr[0] != oneDigit生成命名函数。那里在检查什么

  2. # 2 楼答案

    使用集合fid显示所看到的数字。集合不允许重复

    中的代码应该如下所示:

    Scanner In = new Scanner(System.in);
    int number = In.nextInt();
    int index = 1;
    int newNum;
    String message = "";
    Set<Integer> numbersSeen = new HashSet<Integer>();
    
    while(true)
    {
        newNum = number*index;
        message += "N ="+number*index+".  Now he has seen the digits ";
        while(newNum > 0) {
            numbersSeen.add(newNum % 10);
            newNum /= 10;
        }
        for(Integer seenNum : numbersSeen) {
            message += seenNum +", ";
        }
        System.out.println(message.substring(0,message.length()-2));
        message = "";
        index +=2;
        if(numbersSeen.size() == 10) {
            break;
        }
    }