Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最常出现的数字的计数。。。查找给定数字中出现次数最多的数字_Java_Arrays_Algorithm_String - Fatal编程技术网

Java 最常出现的数字的计数。。。查找给定数字中出现次数最多的数字

Java 最常出现的数字的计数。。。查找给定数字中出现次数最多的数字,java,arrays,algorithm,string,Java,Arrays,Algorithm,String,下面是要执行的代码 查找数字中给定数字的出现次数。要查找给定数字中出现次数最多的数字,我应该做些什么。(我应该创建数组并保存这些值,然后进行比较吗) 谁能帮帮我吗 import java.util.*; public class NumOccurenceDigit { public static void main(String[] args) { Scanner s= new Scanner(System.in);

下面是要执行的代码 查找数字中给定数字的出现次数。要查找给定数字中出现次数最多的数字,我应该做些什么。(我应该创建数组并保存这些值,然后进行比较吗) 谁能帮帮我吗

import java.util.*;
public class NumOccurenceDigit 
{
    public static void main(String[] args) 

        {
            Scanner s= new Scanner(System.in);

            System.out.println("Enter a Valid Digit.(contaioning only numerals)");
            int number = s.nextInt();
            String numberStr = Integer.toString(number);
            int numLength = numberStr.length();

            System.out.println("Enter numer to find its occurence");
            int noToFindOccurance = s.nextInt();
            String noToFindOccuranceStr = Integer.toString(noToFindOccurance);
            char noToFindOccuranceChar=noToFindOccuranceStr.charAt(0);

            int count = 0;
            char firstChar = 0;
            int i = numLength-1;
            recFunNumOccurenceDigit(firstChar,count,i,noToFindOccuranceChar,numberStr);

    }
    static void recFunNumOccurenceDigit(char firstChar,int count,int i,char noToFindOccuranceChar,String numberStr)
    {

        if(i >= 0)
        {
            firstChar = numberStr.charAt(i);
            if(firstChar == noToFindOccuranceChar)
            //if(a.compareTo(noToFindOccuranceStr) == 0)
            {
                count++;

            }
            i--;
            recFunNumOccurenceDigit(firstChar,count,i,noToFindOccuranceChar,numberStr);
        }
        else
        {
            System.out.println("The number of occurance of the "+noToFindOccuranceChar+" is :"+count);
            System.exit(0);
        }
    }
}


/*
 * Enter a Valid Digit.(contaioning only numerals)
456456
Enter numer to find its occurence
4
The number of occurance of the 4 is :2*/

声明计数[]数组

将find函数改为

//for (i = 1 to n)
{
     count[numberStr.charAt(i)]++;
}

然后在count[]中找到最大的项

声明count[]数组

O(n)
将find函数改为

//for (i = 1 to n)
{
     count[numberStr.charAt(i)]++;
}
然后查找计数[]中最大的项目

O(n)
  • 保持
    int位[]=新int[10]
  • 每次遇到
    数字i
    增加
    数字[i]+
  • 返回最大位数数组及其索引。就这些
  • 以下是我的Java代码:

    public static int countMaxOccurence(String s) {
        int digits[] = new int[10];
    
        for (int i = 0; i < s.length(); i++) {
            int j = s.charAt(i) - 48;
            digits[j]++;
        }
    
        int digit = 0;
        int count = digits[0];
        for (int i = 1; i < 10; i++) {
            if (digits[i] > count) {
                count = digits[i];
                digit = i;
            }
        }
    
        System.out.println("digit = " + digit + "  count= " + count);
        return digit;
    }
    
  • 保持
    int位[]=新int[10]
  • 每次遇到
    数字i
    增加
    数字[i]+
  • 返回最大位数数组及其索引。就这些
  • 以下是我的Java代码:

    public static int countMaxOccurence(String s) {
        int digits[] = new int[10];
    
        for (int i = 0; i < s.length(); i++) {
            int j = s.charAt(i) - 48;
            digits[j]++;
        }
    
        int digit = 0;
        int count = digits[0];
        for (int i = 1; i < 10; i++) {
            if (digits[i] > count) {
                count = digits[i];
                digit = i;
            }
        }
    
        System.out.println("digit = " + digit + "  count= " + count);
        return digit;
    }
    
    公共类演示{
    公共静态void main(字符串[]args){
    System.out.println(“结果:+maxOccurDigit(327277));
    }
    公共静态int maxOccurDigit(int n){
    int maxCount=0;
    int maxNumber=0;
    if(n<0){
    n=n*(-1);
    }
    对于(int i=0;i 0){
    如果(数值%10==i){
    计数++;
    }
    num=num/10;
    }
    如果(计数>最大计数){
    最大计数=计数;
    maxNumber=i;
    }else if(count==maxCount){
    maxNumber=-1;
    }
    }
    返回maxNumber;
    }}
    
    上述代码返回给定数字中出现次数最多的数字。如果没有这样的数字,它将返回-1(即,如果有2个或更多的数字出现相同的次数,则返回-1。例如,如果通过323277,则结果为-1)。此外,如果传递了一个带有一位数的数字,则返回该数字本身例如如果数字5通过,则结果为5

    公共类演示{
    公共静态void main(字符串[]args){
    System.out.println(“结果:+maxOccurDigit(327277));
    }
    公共静态int maxOccurDigit(int n){
    int maxCount=0;
    int maxNumber=0;
    if(n<0){
    n=n*(-1);
    }
    对于(int i=0;i 0){
    如果(数值%10==i){
    计数++;
    }
    num=num/10;
    }
    如果(计数>最大计数){
    最大计数=计数;
    maxNumber=i;
    }else if(count==maxCount){
    maxNumber=-1;
    }
    }
    返回maxNumber;
    }}
    
    上述代码返回给定数字中出现次数最多的数字。如果没有这样的数字,它将返回-1(即,如果有2个或更多的数字出现相同的次数,则返回-1。例如,如果通过323277,则结果为-1)。此外,如果传递了一个带有一位数的数字,则返回该数字本身例如如果数字5通过,则结果为5