java实例--海盗的最优方案
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]);
}
}
}
java实例--海盗的最优方案的更多相关文章
- [java] JVM监控与调优
原文出处:http://www.cnblogs.com/zhguang/p/java-jvm-gc.html 光说不练假把式,学习Java GC机制的目的是为了实用,也就是为了在JVM出现问题时分 ...
- Atitti.java android反编译解决方案-----虚拟机方案
Atitti.java android反编译解决方案-----虚拟机方案 哈哈,终极解决方案是虚拟机...c++也可以反编译为汇编代码,但无需担心,因为读懂汇编太麻烦..只要不能拿到c++源码就可.. ...
- 数据分表Mybatis Plus动态表名最优方案的探索
一.应用场景 大家在使用Mybatis进行开发的时候,经常会遇到一种情况:按照月份month将数据放在不同的表里面,查询数据的时候需要跟不同的月份month去查询不同的表. 但是我们都知道,Mybat ...
- Thrift入门及Java实例演示<转载备用>
Thrift入门及Java实例演示 作者: Michael 日期: 年 月 日 •概述 •下载配置 •基本概念 .数据类型 .服务端编码基本步骤 .客户端编码基本步骤 .数据传输协议 •实例演示(ja ...
- Protocol Buffer技术详解(Java实例)
Protocol Buffer技术详解(Java实例) 该篇Blog和上一篇(C++实例)基本相同,只是面向于我们团队中的Java工程师,毕竟我们项目的前端部分是基于Android开发的,而且我们研发 ...
- linux内存基础知识和相关调优方案
内存是计算机中重要的部件之中的一个.它是与CPU进行沟通的桥梁. 计算机中全部程序的执行都是在内存中进行的.因此内存的性能对计算机的影响很大.内存作用是用于临时存放CPU中的运算数据,以及与硬盘等外部 ...
- Java高并发的常见应对方案
Java高并发的常见应对方案 一.关于并发我们说的高并发是什么? 在互联网时代,高并发,通常是指,在某个时间点,有很多个访问同时到来. 高并发,通常关心的系统指标与业务指标? QPS:每秒钟查询量,广 ...
- JAVA实例
JAVA实例1 1 package Demo3; import java.io.File; import java.io.FileReader; import java.io.IOExceptio ...
- Java 实例 - 如何执行指定class文件目录(classpath) Java 实例 J
Java 实例 - 如何执行指定class文件目录(classpath) Java 实例 如果我们 Java 编译后的class文件不在当前目录,我们可以使用 -classpath 来指定class ...
随机推荐
- js获取单选框里面的值
rt,如果想获取单选框里面的值,该如何获取呢. <script> window.onload = function(){ //通过名字获取 getElementsByName //var ...
- centOS 下 VSFTP的安装和设置
http://blog.csdn.net/swiftshow/article/details/7367609 一.FTP的安装 1.检测是否安装了FTP :[root@localhost ~]# rp ...
- Spring+Quartz实现定时任务的配置方法
1.Scheduler的配置 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ...
- Eclipse的模板设置代码
Eclipse Java注释模板设置详解 设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后 ...
- Android空间EditText的InputType属性
android中inputType属性在EditText输入值时启动的虚拟键盘的风格有着重要的作用.这也大大的方便的操作.有时需要虚拟键盘只为字符或只为数字.所以inputType尤为重要. < ...
- invert
http://docs.ruby-lang.org/en/2.0.0/Hash.html invert → new_hash Returns a new hash created by using h ...
- BestCoder Round #60 1002
Problem Description You are given two numbers NNN and MMM. Every step you can get a new NNN in the w ...
- Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- wsp反编译
最后出于好奇,我把wsp文件解压缩,看看里面是什么(如果您的机器上的压缩软件不能直接解压,可尝试修改后缀名为cab.).我看到的首先是一个清单文件(manifest.xml),一个DLL文件(Shar ...
- TypeError: document.formname.submit is not a function
<form name="formname" ...> .... <input name="submit" type="submit& ...