軟體工程個人作業13

來源:互聯網
上載者:User

標籤:

•三人行設計了一個灌水論壇。資訊學院的學生都喜歡在上面交流灌水,傳說在論壇上有一個“水王”,他不但喜歡發帖,還會回複其他ID發的每個文章。坊間風聞該“水王”發帖數目超過了貼文數目的一半。•如果你有一張當前論壇的文章(包括回帖)列表,其中文章的作者的ID也在其中,你能快速的找到這個傳說中的水王嗎?
•隨著論壇的發展,管理員發現水王沒有了,但是統計結果表明,有三個發帖很多的ID。據統計他們的發帖數量超過了1/4,你能從發帖列表中快速找到他們嗎?設計思想之前“水王”發帖數目超過了貼文數目的一半,所以直接找貼文數超過一半的ID即可,這次找第二個“水王”只需要在把ID的列表去掉原來的“水王”,再次尋找即可。代碼實現:
 1 package finding; 2 import java.util.InputMismatchException; 3 import java.util.Scanner; 4  5 public class finding { 6     public static void main(String[] args) { 7         // TODO 自動產生的方法存根 8         for(int m=0;;) 9         {10             int bug=0;11             Scanner sc =new Scanner(System.in);12             System.out.println("請輸入ID的個數:");13             int n=sc.nextInt();14             System.out.println("請輸入ID(為整數):");15             int ID[]=new int [n];16             try                              //捕捉輸入錯誤17             {18                 for(int i=0;i<n;i++)19                 {20                     ID[i]=sc.nextInt();21                 }22             }23             catch(InputMismatchException e)24             {25                  System.out.println("輸入不合法!請輸入整數!");26                  bug=1;27              }28             if(bug!=1)29             {30                 int shuiwang[]=new int [2];31                 for(int l=0;l<2;l++)32                 {33                     shuiwang[l]=ID[0];                 //從第一個ID開始,第一個即為水王34                     int temp=1;35                     for(int i=0;i<n-1;i++)36                     {37                         for(int j=i+1;j<n;j++)38                         {39                             if(shuiwang[0]==ID[j])         //當此時水王與下一個ID相同時,計數器temp+140                             {41                                 temp++;42                             }43                         }44                         if(temp<=n/2)45                         {46                             shuiwang[0]=ID[i+1];         //當temp小於總貼文數的一半時,換下一個ID為臨時水王47                         }48                     }49                     for(int i=0;i<n-temp;i++)50                     {51                         for(int j=0;j<n;j++)52                         {53                             if(ID[j]!=shuiwang[0])54                             {55                                 ID[i]=ID[j];56                                 break;57                             }58                              59                         } 60                      }61                      n=n-temp;62                 }                63                  System.out.println("水王是(發帖數超過總貼數一半的ID):"+shuiwang[0]+"   "+shuiwang[1]);64              }65             System.out.println("繼續尋找水王請按n,退出請輸入s:");66             String s=sc.next();67             if(s.equals("s"))68                 System.exit(0);69             else70                 continue;    71             sc.close();72         }73 74     }75 }

 

軟體工程個人作業13

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.