軟體工程練習——找水王2

來源:互聯網
上載者:User

標籤:

    隨著論壇的發展,管理員發現水王沒有了,但是統計結果表明,有三個發帖很多的ID。據統計他們的發帖數量超過了1/4,你能從發帖列表中快速找到他們嗎?

    設計思想:這次的水王有三個,所以我選擇用排除法一個個進行排除,假設第一個ID是水王,用它與之後的其他ID進行比較,對出現的次數進行統計,最後將這個ID全部拿出來,放入另一個數組中(出棧入棧的思想)。然後迴圈這個過程,直到全部水王找出。

package shuiwang2;import java.util.*;public class Find_shuiwang {    public static void main(String[] args) {        // TODO Auto-generated method stub        Find_function function = new Find_function();        Scanner sca1=new Scanner(System.in);        Scanner sca2=new Scanner(System.in);        System.out.println("輸入貼文數:");        int n=sca1.nextInt();        String post[]=new String[n];                int i;        System.out.println("輸入文章ID:");        for(i=0;i<n;i++)        {              post[i]=sca2.nextLine();        }        String shuiwang[] = function.find(post, n);        System.out.println("水王ID是:");        for(i=0;i<3;i++)        {            System.out.println(shuiwang[i]+" ");        }            }}
Find_shuiwang
package shuiwang2;public class Find_function {    String[] find(String[] post,int n)    {        int i,j = 0;        int num_find=0;//找到幾個相同的ID        int num_mark=0;//標記有幾個ID入棧        int num_shuiwang=0;                String shuiwang[]= new String[3];        String zhan[]=new String[n+1];//建一個棧放ID                        while(num_shuiwang<3){//當水王沒找全之前一直迴圈            shuiwang[num_shuiwang]=post[0];//令第一個ID為水王            for(i=0;i<n-num_mark;i++)//兩兩對比            {                            if(shuiwang[num_shuiwang].equals(post[i]))                    {                        zhan[num_mark]=post[i];                        num_find+=1;                                                for(j=i;j<n-num_mark-1;j++)//前移數組                        {                            post[j]=post[j+1];                        }                        num_mark+=1;                    }                        }            if(num_find >= n/4)            {                num_shuiwang+=1;                num_find = 0;            }            else            {                num_find = 0;            }        }        return shuiwang;    }}
Find_function

    結果:

軟體工程練習——找水王2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.