2013-05-05 45 views
0

我需要從命令行取單個數字的整數,並將它們放入數組中,然後查找最頻繁的整數。有時這個程序似乎有效,而其他時間則不行。如何從命令行中找到數組中最頻繁重複的數字?

public class MostFrequent { 
    public static void main(String[] args){ 
    int num=0; 
    int[] freq= new int[9];//intialize array for integers 0-9 
    for (int i=0; i<args.length; i++){ 
     try { 
     num = Integer.parseInt(args[i]); 
     freq[num]++;//adds to array counter 
     } 
     catch (NumberFormatException nfe) { 
     } 
    } 
    int max=0,j; 
    for (j=0; j<10; j++){ 
     while(freq[j]>max){ 
     max=freq[j]; 
     } 
    } 
    System.out.println("The digit that appears most frequently is " + freq[j]); 
    } 
} 

謝謝大家對你的幫助,這是結束了,我的工作,並感謝誰提到使陣列更具動感,這有助於爲好。 這裏是我完成的代碼:

public class MostFrequent { 
public static void main(String[] args){ 
int num=0; 
int[] freq= new int[args.length];//intialize array for integers 0-9 
for (int i=0; i<args.length; i++){ 
    try { 
    num = Integer.parseInt(args[i]); 
    freq[num]++;//adds to array counter 
    } 
    catch (NumberFormatException nfe) { 
    } 
} 
int max=0,j; 
for (j=1; j<args.length; j++){ 
    while(freq[j]>freq[max]){//compares a max array val to the previous val 
    max=j; 
    } 
} 
System.out.println("The digit that appears most frequently is " + max); 

}}

+4

當它不起作用時會發生什麼? – Keppil 2013-05-05 18:09:42

+0

我遇到了數組綁定錯誤,我認爲這是打印輸入數字的次數,而不是顯示那個數字。我相信我現在已經修復了 – dcole617 2013-05-05 18:36:28

回答

2

在你的第二個循環的邏輯是有缺陷的。此外,您還沒有爲陣列中的所有數字分配空間,因此您需要一個int[10]。解決這個問題的方法之一是這樣的:

int[] freq = new int[10];//intialize array for integers 0-9 

... 

int maxindex = 0; 
for (int j = 1; j < 10; j++){ 
    if (freq[j] > freq[maxIndex]) { 
     maxIndex = j; 
    } 
} 
System.out.println("The digit that appears most frequently is " + j + ", that appears " + freq[j] + " times."; 
+0

我改變了你的第一行以允許任何長度 > int [] freq = new int [args.length]; – dcole617 2013-05-05 18:33:13

0

更改你的循環

int max=freq[0]; 
int maxIndex = 0; 
for (j=1; j<10; j++){ 
    if(freq[j]>max) 
    { 
    max=freq[j]; 
    maxIndex = j; 
    } 
} 

而且,你有錯輸出。

而不是(這將使你的最後一個數字)

System.out.println("The digit that appears most frequently is " + freq[j]); 

使用(用於打印最大數量及其occurence數)

System.out.println("The digit that appears most frequently is " + maxIndex + " - " + max + "x"); 
+0

謝謝,我最終改變了循環爲 > for(j = 1; j dcole617 2013-05-05 18:34:23

0

這裏找到最頻繁的號碼實現。

#include<iostream> 
using namespace std; 

// Returns maximum repeating element in arr[0..n-1]. 
// The array elements are in range from 0 to k-1 
int maxRepeating(int* arr, int n, int k) 
{ 
// Iterate though input array, for every element 
// arr[i], increment arr[arr[i]%k] by k 
for (int i = 0; i< n; i++) 
    arr[arr[i]%k] += k; 

// Find index of the maximum repeating element 
int max = arr[0], result = 0; 
for (int i = 1; i < n; i++) 
{ 
    if (arr[i] > max) 
    { 
     max = arr[i]; 
     result = i; 
    } 
} 

/* Uncomment this code to get the original array back 
    for (int i = 0; i< n; i++) 
     arr[i] = arr[i]%k; */ 

// Return index of the maximum element 
return result; 
} 

// Driver program to test above function 
int main() 
{ 
int arr[] = {2, 3, 3, 5, 3, 4, 1, 7}; 
int n = sizeof(arr)/sizeof(arr[0]); 
int k = 8; 

cout << "The maximum repeating number is " << 
    maxRepeating(arr, n, k) << endl; 

return 0; 
} 
相關問題