Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.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_Boyer Moore - Fatal编程技术网

Java 查找输入数组多数元素的程序不';行不通

Java 查找输入数组多数元素的程序不';行不通,java,boyer-moore,Java,Boyer Moore,我试图用Boyer和Moore的方法找到大多数元素。程序应该要求用户在第一行输入“n”行数,然后在“n”行后面有“n”个数字作为输入。(例如:用户在第一行输入5,随后将有5个数字) 接下来,使用Boyer和Moore方法查找输入数组的多数元素。如果输入数组中不存在多数元素,则执行-1。无论我输入什么输入,我的程序输出都显示为0。请你检查一下并更正我的程序好吗 输出示例: 4. 12 12 1. 12 12 /或:3 11 2 13-1 public static void main (Strin

我试图用Boyer和Moore的方法找到大多数元素。程序应该要求用户在第一行输入“n”行数,然后在“n”行后面有“n”个数字作为输入。(例如:用户在第一行输入5,随后将有5个数字) 接下来,使用Boyer和Moore方法查找输入数组的多数元素。如果输入数组中不存在多数元素,则执行-1。无论我输入什么输入,我的程序输出都显示为0。请你检查一下并更正我的程序好吗

输出示例: 4. 12 12 1. 12 12 /或:3 11 2 13-1

public static void main (String[]args)
{
    int a[] = new int [1000000];
    Scanner sc = new Scanner (System.in);

    // User input n number of lines
    int numberOfLine = sc.nextInt();

    //Loop to have n elements as input
    for (int i = 1; i<= numberOfLine; i++)
    {
        a[i] = sc.nextInt();
    }

    // Call method to display majority element
    getMajorityElement(a);
    sc.close();
}       

//Method to Find M.E using Boyer & Moore approach
public static void getMajorityElement(int [] array)
{
    int majorityElement = array[0];
    int count = 1;
    for (int index = 1; index<array.length; index++)
    {
        if(majorityElement==array[index])
        {
            count++;
        }
        else if(count==0)
        {
            majorityElement = array[index];
            count = 1;
        }
        else 
        {
            count --;
        }
    }

    // Check if candidate M.E occur more than n/2 times
    count = 0;
    for (int index = 0; index<array.length; index++)
    {
        if(array[index]==majorityElement)
            {
            count++;
            }
    }
        if (count > array.length/2)
        {
        System.out.println(majorityElement);
        }
        else
        {
        System.out.println("-1");
        }
}
publicstaticvoidmain(字符串[]args)
{
整数a[]=新整数[1000000];
扫描仪sc=新的扫描仪(System.in);
//用户输入n行数
int numberOfLine=sc.nextInt();
//循环以n个元素作为输入

对于(int i=1;i您获得此行为的原因是1000000个元素的数组的多数元素为零:只设置了最初的三个或四个项,而其余的项被零占用—Java中
int
的默认值

通过在输入长度后按大小分配来修复此问题。您还需要修复读取输入的代码,以确保数据最终位于索引
0..numberOfLine-1

Scanner sc = new Scanner (System.in);
// User input n number of lines
int numberOfLine = sc.nextInt();
int a[] = new int [numberOfLine];
//Loop to have n elements as input
for (int i = 0 ; i < numberOfLine ; i++) {
    a[i] = sc.nextInt();
}
Scanner sc=新扫描仪(System.in);
//用户输入n行数
int numberOfLine=sc.nextInt();
int a[]=新int[numberOfLine];
//循环以n个元素作为输入
对于(int i=0;i
你的代码毫无意义。你为什么要在循环中进行减法,以找出发生的次数?我想你真的想得太多了……很多。@Stultuske这是Boyer-Moore算法的核心,这是一种非常不直观的算法,但有数学证明它确实有效。谢谢你,b但是我想你的意思是for循环应该是for(int I=0;I