軟體工程作業--找水王續

來源:互聯網
上載者:User

標籤:

題目要求:

•三人行設計了一個灌水論壇。資訊學院的學生都喜歡在上面交流灌水,傳說在論壇上有一個“水王”,他不但喜歡發帖,還會回複其他ID發的每個文章。坊間風聞該“水王”發帖數目超過了貼文數目的一半。•如果你有一張當前論壇的文章(包括回帖)列表,其中文章的作者的ID也在其中,你能快速的找到這個傳說中的水王嗎?(參考核心代碼)•隨著論壇的發展,管理員發現水王沒有了,但是統計結果表明,有三個發帖很多的ID。據統計他們的發帖數量超過了1/4,你能從發帖列表中快速找到他們嗎?實現思路:1.還是將問題抽象化,目前有一組資料,其中有大於等於4分之1的資料是一個確定的值,而且這樣的確定的值有三個,剩下的不到四分之一的值都不重複。

2.這樣就好辦了,還是按照第一次找水王的思路來就可以啦,不過這次的傳回值是三個,需要用到數組來解決,nTimes 的3個元素分別對應當前遍曆過的3個ID出現的 個數。如果遍曆中有某個ID不同於這3個當前ID,我們就判斷當前3個ID是否有某個的nTimes為0,如果有,那這個新遍曆的ID就取而代之,並賦1 為它的遍曆數(即nTimes減1),如果當前3個ID的nTimes皆不為0,則3個ID的nTimes皆減去1,這也就是解決本文題的關鍵了。由於非 水王ID不滿總數的1/4,與上題思路相同,所遍曆ID與當前3個ID不同時,就一同抵消(即3個當前ID的nTimes值減1),最終留下來的3個當 前ID總會是3個超過1/4的水王ID。

代碼實現:

 

  1 public class Find {  2     static int[] ID=new int[100];  3     int Rand()//產生0-1內的隨機數  4     {  5         java.util.Random random=new java.util.Random();// 定義隨機類  6         int result=random.nextInt(4);// 返回[0,4)集合中的整數,注意不包括4  7         return result;  8     }  9     void FirstC()//初始化數組; 10     { 11         for(int i=0;i<100;i++) 12         { 13             if(Rand()==0) 14             { 15                 ID[i]=50;//水王一號的ID 16             } 17             else if(Rand()==1) 18             { 19                 ID[i]=60;//水王二號的ID 20             } 21             else if(Rand()==2) 22             { 23                 ID[i]=70;//水王三號的ID 24             } 25             else 26             { 27                 ID[i]=i; 28             } 29              30         } 31      32     } 33     void Out() 34     { 35         System.out.println("以下是水貼的ID單"); 36         for(int i=1;i<=ID.length;i++) 37         { 38             System.out.print(ID[i-1]+" "); 39             if(i%10==0) 40             { 41                 System.out.print("\n"); 42             } 43         } 44     } 45 /*    int Serch()//思想實現部分 46     { 47         int candIDate=50; 48         int ntimes,i; 49         for(i = ntimes = 0;i<ID.length;i++){ 50           if(ntimes == 0){ 51             candIDate = ID[i]; 52             ntimes = 1; 53           } 54           else{ 55             if(candIDate == ID[i]) 56               ntimes ++; 57             else  58               ntimes --; 59           } 60         } 61         return candIDate; 62     }*/ 63     public static int[]  Serchs(int[] ID) 64     { 65          66                 if(ID == null) return null;   67                 int candate0= 0;   68                 int candate1= 0;   69                 int candate2= 0;   70                 int times0 = 0;   71                 int times1 = 0;   72                 int times2 = 0;   73                 int len = ID.length;   74                 for(int i =0;i<len;i++){   75                     if(times0==0&&ID[i]!=candate1&&ID[i]!=candate2){   76                         candate0=ID[i];   77                         times0++;   78                     }   79                     else if(times1==0&&ID[i]!=candate0&&ID[i]!=candate2){   80                         candate1=ID[i];   81                         times1++;   82                     }   83                     else if(times2==0&&ID[i]!=candate0&&ID[i]!=candate1){   84                         candate2=ID[i];   85                         times2++;   86                     }   87                     else if(ID[i]!=candate1&&ID[i]!=candate0&&ID[i]!=candate2){   88                         times0--;   89                         times1--;   90                         times2--;   91                     }   92                     else if(ID[i]==candate0)   93                         times0++;   94                     else if(ID[i]==candate1)   95                         times1++;   96                     else if(ID[i]==candate2)   97                         times2++;   98                 }   99                int[] result = {candate0,candate1,candate2};  100                 return result;  101             }  102           103     @SuppressWarnings("static-access")104     public static void main(String[] args) {105         Find a = new Find();106         a.FirstC();107         a.Out();108         int[] results=new int[3];109         results=a.Serchs(ID);110         System.out.println("三個水王的ID分別是");111         for(int i=0;i<3;i++)112         {113             System.out.println("第"+(i+1)+"個水王的ID是"+results[i]);114         }115         116     }117 118 }

 

運行結果:

修改水王ID再次運行程式,觀察結果:

運行結果:

 

心得體會:

1.思想非常重要,

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.