JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 印出陣列內重複數字出現的次數
evian





發文: 10
積分: 0
於 2018-09-06 16:44 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
2.  利用一維陣列儲存以下數字,[18, 22, 34, 2, 18, 15, 35, 15, 18, 2, 27],由小到大印出其內容,不印出重複數字,但印出出現次數。
[程式輸出]
  2(2) 15(2) 18(3) 22 27 34 35

以下是我目前打出來的程式 輸出為2 15 18 22 27 34 35 但是要如何算出重複的次數呢?

public class Test2 {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[]a= {18, 22, 34, 2, 18, 15, 35, 15, 18, 2, 27};
    int min=a[0];
    for(int i=a.length;i>0;i--) {
      for(int j=0;j<i-1;j++) {
        if(a[j+1]<a[j]) {
          int temp=a[j];
          a[j]=a[j+1];
          a[j+1]=temp;
        }
      }
    }
    int cnt=0;
    for(int i=0;i<a.length-1;i++) {
        if(a[i+1]==a[i]) {
          continue;
        }
        System.out.print(a[i]+" ");
    }
  }

}


reply to postreply to post
作者 Re:印出陣列內重複數字出現的次數 [Re:evian]
ggininder





發文: 35
積分: 0
於 2018-09-07 22:47 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
比較簡單的做法就是每個數搜尋時 每個樹後面跟一個count 在每搜尋時都去跟曾經搜尋過的數比對,比對搜尋過就count+1,然後在印出來。

reply to postreply to post
作者 Re:印出陣列內重複數字出現的次數 [Re:evian]
ryanpai





發文: 47
積分: 0
於 2018-09-08 15:10 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以考慮用 R + RCaller ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import com.github.rcaller.rstuff.RCaller;
import com.github.rcaller.rstuff.RCode;
 
public class ExampleRcaller {
 
    public static void main(String[] args) {
        try {
            RCaller caller = RCaller.create();
            RCode code = RCode.create();
 
            code.addRCode("numbers <- c(18, 22, 34, 2, 18, 15, 35, 15, 18, 2, 27)");
            code.addRCode("result <- as.data.frame(table(numbers))");
            caller.setRCode(code);
            caller.runAndReturnResult("result");
            System.out.println(caller.getParser().getNames()); // => [numbers, Freq]
 
            double[] numbers = caller.getParser().getAsDoubleArray("numbers");
            double[] Freq = caller.getParser().getAsDoubleArray("Freq");
 
            String numbers_str = "[num]\t";
            String Freq_str = "[Freq]\t";
            for (int i = 0; i < Freq.length; i++) {
                numbers_str = numbers_str + (int) numbers[i] + "\t";
                Freq_str = Freq_str + (int) Freq[i] + "\t";
            }
            System.out.println("---------------------------------------------------------------");
            System.out.println(numbers_str);
            System.out.println(Freq_str);
 
        } catch (Exception e) {
            System.out.println("Exception:" + e);
        }
    }
}


run:
[numbers, Freq]
---------------------------------------------------------------
[num]  2  15  18  22  27  34  35  
[Freq]  2  2  3  1  1  1  1


reply to postreply to post
作者 Re:印出陣列內重複數字出現的次數 [Re:ryanpai]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2018-09-19 14:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ryanpai wrote:
可以考慮用 R + RCaller ...


那跟用R寫沒兩樣 Big Smile。如果是這樣,我也可以說 "可以考慮用 APL"

{(⍪∪x),+/(∪x)∘.⍷x←⍵[⍋⍵]} 18 22 34 2 18 15 35 15 18 2 27

2 2
15 2
18 3
22 1
27 1
34 1
35 1


reply to postreply to post
作者 Re:印出陣列內重複數字出現的次數 [Re:T55555]
ryanpai





發文: 47
積分: 0
於 2018-10-05 09:16 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
說的是,我是想說多元的運用會對想法與做法有一點點幫助...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
public class ArrayTest {
    public static void main(String[] args) {
        int[] a = {18, 22, 34, 2, 18, 15, 35, 15, 18, 2, 27};
        int[] b = Arrays.stream(a).sorted().distinct().toArray();
        System.out.println(Arrays.toString(b));
        int[] c = new int[b.length];
        List<Integer> list = Arrays.stream(a).boxed().collect(Collectors.toList());
        for (int i = 0; i < b.length; i++) {
            c[i] = Collections.frequency(list, b[i]);
        }
        System.out.println(Arrays.toString(c));
    } // end main
} // end ArrayTest
 
Tongue


reply to postreply to post
作者 Re:印出陣列內重複數字出現的次數 [Re:evian]
clock

小時候在看國外測試機器人的影片



發文: 65
積分: 0
於 2018-10-05 13:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
試試中國古寫法 XD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ClockNumberCount {
 
    public static void main(String[] args) {
        int[] numberAry = {18, 22, 34, 2, 18, 15, 35, 15, 18, 2, 27};
 
        Arrays.sort(numberAry);
        int count = 1;
        int temp = numberAry[0];
        String resultStr="";
        for (int i = 1; i < numberAry.length; i++) {
            if (temp == numberAry[i]) {
                count++;
            } else {               
                resultStr+=temp + ":" + count + "次"+"\n";           
                temp = numberAry[i];
                count = 1;
            }
            if (i == numberAry.length - 1) {
                   resultStr+=temp + ":" + count + "次";              
            }
        }
         System.out.println(resultStr);
 
    }
 
}


reply to postreply to post
c:你好像很喜歡研究機械產品
X:我從小就跟博士做研究
c:哪位博士
X:南宮博士
作者 Re:印出陣列內重複數字出現的次數 [Re:ryanpai]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2018-10-05 15:32 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好吧,即然你寫了java, 我也來, all way stream... ( 樓主如果看不懂, 那去Java 新手區吧)
1
2
3
4
5
6
7
8
9
10
11
12
13
import static java.util.function.Function.*;
import static java.util.stream.Collectors.*;
import java.util.*;
 
public class Fun {
  public static void main(String[] args) {
    int[] a = {18, 22, 34, 2, 18, 15, 35, 15, 18, 2, 27};
    Arrays.stream(a)
          .boxed()
          .collect(groupingBy(identity(), TreeMap::new, counting()))
          .forEach((k, v) -> System.out.print(" " + k + (v > 1 ? "(" + v + ")" : "")));
  }
}


Result:
1
 2(2) 15(2) 18(3) 22 27 34 35


T55555 edited on 2018-10-05 16:02
reply to postreply to post
作者 Re:印出陣列內重複數字出現的次數 [Re:T55555]
ryanpai





發文: 47
積分: 0
於 2018-10-06 07:18 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
版主大真厲害 Smile

reply to postreply to post
» JWorld@TW »  Java SE 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8