欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)的海盜算法優(yōu)化版

 更新時(shí)間:2017年07月14日 09:02:22   作者:超級(jí)學(xué)渣渣  
這篇文章主要介紹了java實(shí)現(xiàn)的海盜算法優(yōu)化版,結(jié)合實(shí)例形式分析了java海盜算法的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了java實(shí)現(xiàn)的海盜算法。分享給大家供大家參考,具體如下:

前面介紹了《C#實(shí)現(xiàn)的海盜分金算法》,這里再給出一個(gè)Java優(yōu)化版的算法:

package unit4;
public class Pirate{
  private String name;
  private int[] schemes;
  private int index;
  public Pirate(int t,int i) {
    name="unknow";
    index=i;
    schemes=makeSchemes(t);
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getIndex(){return index;}
  public int[] getSchemes() {return schemes;}
  public int handvote(int table[]) {
    return myhandvote(table,index);
  }
  private int myhandvote(int []t,int i) {
    if(t[i]==0) return 0;
    if(i==1) return 0;
    return 1;
  }
  public int[] makeSchemes(int t) {
    int vote=0;
    schemes =new int[t-index];
    do{
    for(int i=0;i<schemes.length;i++){
      schemes[i]=(i+1)%2;
    }
    for(int i=0;i<schemes.length;i++){
      vote=vote+myhandvote(schemes,i);
      }
    }while(!(2*vote>t/2));
    return schemes;
  }
}

package unit4;
public class Judger {
  int[] allot;
  Pirate[] pirates;
  public Judger(Pirate []pirates,int[]a) {
    this.pirates=pirates;
    allot=a;
  }
  public int[] getAllot() {
    return allot;
  }
  public void setAllot(int[] allot) {
    this.allot = allot;
  }
  public Pirate[] getPirates() {
    return pirates;
  }
  public void setPirates(Pirate[] pirates) {
    this.pirates = pirates;
  }
  public boolean evaluate(){
    int vote=0;
    for(int i=0;i<pirates.length;i++){
      vote+=pirates[i].handvote(allot);
    }
    if(2*vote>=pirates.length) return true;
    else return false;
  }
}

package unit4;
public class MaxInterest {
  public static void main(String[] args) {
    int piratecounts=5;
    Pirate[]pirates=new Pirate[piratecounts];
    for(int i=0;i<piratecounts;i++){
      pirates[i]=new Pirate(piratecounts,i);
      pirates[i].setName("name"+i);
    }
    int[]table =pirates[0].getSchemes();
    Judger ajudge=new Judger(pirates,table);
    if(ajudge.evaluate()){
      int[]scheme=ajudge.getAllot();
      for(int i=0;i<scheme.length;i++) System.out.println(" "+scheme[i]);
    }
  }
}

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • SpringBoot跨域Access-Control-Allow-Origin實(shí)現(xiàn)解析

    SpringBoot跨域Access-Control-Allow-Origin實(shí)現(xiàn)解析

    這篇文章主要介紹了SpringBoot跨域Access-Control-Allow-Origin實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot集成slf4j+log4j2的示例代碼

    SpringBoot集成slf4j+log4j2的示例代碼

    這篇文章主要介紹了SpringBoot集成slf4j+log4j2的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • JAVA中STRING的常用方法小結(jié)

    JAVA中STRING的常用方法小結(jié)

    這篇文章介紹了JAVA中STRING的常用方法,有需要的朋友可以參考一下
    2013-09-09
  • 在Springboot中Mybatis與Mybatis-plus的區(qū)別詳解

    在Springboot中Mybatis與Mybatis-plus的區(qū)別詳解

    MyBatis是一個(gè)優(yōu)秀的持久層框架,它對(duì)JDBC的操作數(shù)據(jù)庫的過程進(jìn)行封裝,MyBatisPlus (簡稱 MP)是一個(gè) MyBatis的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,本文將給大家介紹了在Springboot中Mybatis與Mybatis-plus的區(qū)別
    2023-12-12
  • Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能

    Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能

    這篇文章主要介紹了Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能,使用步驟是先創(chuàng)建一個(gè)線程池的配置,讓Spring Boot加載,用來定義如何創(chuàng)建一個(gè)ThreadPoolTaskExecutor,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-08-08
  • SpringBoot+MyBatis+AOP實(shí)現(xiàn)讀寫分離的示例代碼

    SpringBoot+MyBatis+AOP實(shí)現(xiàn)讀寫分離的示例代碼

    高并發(fā)這個(gè)階段,肯定是需要做MySQL讀寫分離的。本文主要介紹了SpringBoot+MyBatis+AOP實(shí)現(xiàn)讀寫分離的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • spring-boot-klock-starter V1.1 主體功能重大更新內(nèi)容介紹

    spring-boot-klock-starter V1.1 主體功能重大更新內(nèi)容介紹

    這篇文章主要介紹了spring-boot-klock-starter V1.1 主體功能重大更新內(nèi)容描述,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • java連接池Druid連接回收DestroyConnectionThread&DestroyTask

    java連接池Druid連接回收DestroyConnectionThread&DestroyTask

    這篇文章主要為大家介紹了java連接池Druid連接回收DestroyConnectionThread&DestroyTask示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • rocketmq如何修改存儲(chǔ)路徑

    rocketmq如何修改存儲(chǔ)路徑

    這篇文章主要介紹了rocketmq如何修改存儲(chǔ)路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java如何實(shí)現(xiàn)樹形查詢

    java如何實(shí)現(xiàn)樹形查詢

    這篇文章主要介紹了java實(shí)現(xiàn)樹形查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07

最新評(píng)論