最大连续邮资问题的JAVA实现
来源:互联网 发布:手机表白网站源码 编辑:程序博客网 时间:2024/06/01 08:48
public class StampCost {
int m;
int n;
int stamps[];
int maxNum = 0;
int[] tempset;
int current;
int rightPos;
int base;
public StampCost(int m,int n,int[] stamps){
this.m = m;
this.n = n;
this.stamps = stamps;
check();
printResult();
}
public void check(){
tempset = new int[stamps[(stamps.length-1)]*4+1];
maxNum = m;//因为第一个确定是1,所以初始化最大的面值就是 1*m
for (int i=1;i<5;i++){
int min = Math.max(stamps[i],maxNum)+1;;
int max = stamps[i]*m;
if (min < stamps[i]+1){
return;
}
maxNum = min-1;
tempset[maxNum]=1;
if (!checkSpan(min,max,i)){
return;
}
}
}
public boolean checkSpan(int min,int max,int rightPos){
base = min;
current=stamps[rightPos];
this.rightPos = rightPos;
backtrace(0);
if (tempset[min] == 0){
return false;
}
for (int i=min;i<max;i++){
if (tempset[i] == 0){
return true;
}
else{
maxNum++;
}
}
return true;
}
public void backtrace(int i){
if (i >= (m-1)){
return;
}
else{
for (int j=0;j<rightPos+1;j++){
current += stamps[j];
if (current >= base){
tempset[current] = 1;
}
backtrace(i+1);
current -= stamps[j];
}
}
}
public void printResult(){
System.out.println(maxNum);
}
public static void main(String args[]){
int m=4;
int n=5;
int[] stamps={1,3,11,15,32};
new StampCost(m,n,stamps);
}
}
int m;
int n;
int stamps[];
int maxNum = 0;
int[] tempset;
int current;
int rightPos;
int base;
public StampCost(int m,int n,int[] stamps){
this.m = m;
this.n = n;
this.stamps = stamps;
check();
printResult();
}
public void check(){
tempset = new int[stamps[(stamps.length-1)]*4+1];
maxNum = m;//因为第一个确定是1,所以初始化最大的面值就是 1*m
for (int i=1;i<5;i++){
int min = Math.max(stamps[i],maxNum)+1;;
int max = stamps[i]*m;
if (min < stamps[i]+1){
return;
}
maxNum = min-1;
tempset[maxNum]=1;
if (!checkSpan(min,max,i)){
return;
}
}
}
public boolean checkSpan(int min,int max,int rightPos){
base = min;
current=stamps[rightPos];
this.rightPos = rightPos;
backtrace(0);
if (tempset[min] == 0){
return false;
}
for (int i=min;i<max;i++){
if (tempset[i] == 0){
return true;
}
else{
maxNum++;
}
}
return true;
}
public void backtrace(int i){
if (i >= (m-1)){
return;
}
else{
for (int j=0;j<rightPos+1;j++){
current += stamps[j];
if (current >= base){
tempset[current] = 1;
}
backtrace(i+1);
current -= stamps[j];
}
}
}
public void printResult(){
System.out.println(maxNum);
}
public static void main(String args[]){
int m=4;
int n=5;
int[] stamps={1,3,11,15,32};
new StampCost(m,n,stamps);
}
}
- 最大连续邮资问题的JAVA实现
- 连续邮资问题的实现代码
- 连续邮资问题的解决办法
- 算法java实现--回溯法-连续邮资问题--子集树
- 连续邮资问题的回溯法解决办法
- 连续邮资问题
- 连续邮资问题
- 连续邮资问题
- 连续邮资问题
- 连续邮资问题
- 连续邮资问题
- 连续邮资问题
- UVA165连续邮资问题
- 连续邮资问题
- 连续邮资问题
- 连续邮资问题
- 算法 连续邮资问题
- 连续邮资问题
- JAVASCRIPT弹出DIV
- arithmetical
- loveapple的开发已经过去了2个月
- System.Globalization 命名空间
- C#实现顺序表
- 最大连续邮资问题的JAVA实现
- 中小企业数据容灾并非遥不可及oracle sql
- javascript nextSibling遇到空格的问题
- 构建Oracle高可用环境HA rac:企业级高可用数据库架构、实战与经验总结
- 数据存储管理
- oracle数据库的备份与恢复
- 数据库迁移几种方式
- 二进制与十进制转换
- sql中创建主键非聚集索引和聚集索引