01 背包
void bag01(int cost,int weight) { for(i=v;i>=cost;i--) if(dp[i]<dp[i-cost]+weight) dp[i]=dp[i-cost]+weight; }
完全背包
void complete(int cost,int weight) { for(i=cost;i<=v;i++) if(dp[i]<dp[i-cost]+weight) dp[i]=dp[i-cost]+weight; }
多重背包
void multiply(int cost,int weight,int amount) { if(cost*amount>=v) complete(cost,weight); else{ k=1; while(k<amount){ bag01(k*cost,k*weight); amount-=k; k+=k; } bag01(cost*amount,weight*amount); } }
一、解析json对象
表结构信息对象,json格式,名称为tableObj
* {
* "tableName":"t_res", //表名称
* "columnInfo":[ //字段信息
* {
* "columnName":"id", //字段名
* "dataTypeName":"varchar", //字段类型
* "isKey":true, //是否为主键,true代表是主键,false代表不是
* "isAutoIncrement":true, //是否自增,true代表自增,false代表不自增
* "isNull":0, //是否为空,1代表可以为空,0代表不能为空
* "precision":5, //精度
* "defaultValue":"10" //默认值
* "scale":2 //小数位数
* }
* ]
* }
try{
String name = tableObj.get("tableName").toString(); //获得表名
JSONArray columnInfo = tableObj.getJSONArray("columnInfo"); //获得字段描述json数组
int size = columnInfo.length(); //json数组长度
for(int i=0;i<size;i++){
JSONObject info=columnInfo.getJSONObject(i);
String cloumn = info.getString("columnName"); //获取字段名
String dataType = info.getString("dataTypeName"); //获取字段类型
boolean isKey = info.getBoolean("isKey"); //获取字段是否为主键
boolean isAutoIncrement = info.getBoolean("isAutoIncrement"); //获取字段是否自增
int isNull = info.getInt("isNull"); //获取字段是否为空
int precision = info.getInt("precision"); //获取字段类型精度
String defaultValue = info.getString("defaultValue"); //获取字段默认值
}
二、封装json对象
/**
* 根据表名获取表结构信息
* @param tableName 表名
* @return 表结构信息对象 Json格式
* {
* "tableName":"t_res", //表名称
* "columnInfo":[ //字段信息
* {
* "columnName":"id", //字段名
* "dataTypeName":"varchar", //字段类型
* "isKey":true, //是否为主键,true代表是主键,false代表不是
* "isAutoIncrement":true, //是否自增,true代表自增,false代表不自增
* "isNull":0, //是否为空,1代表可以为空,0代表不能为空
* "precision":5, //精度
* "defaultValue":"10" //默认值
* "scale":2 //小数位数
* }
* ]
* }
*/
JSONObject tableInfoObj = new JSONObject();
StringBuffer sb = new StringBuffer();
sb.append("{"+"'tableName':"+tableName+","+"'columnInfo':"+"[");
sb.append("{"+"'columnName':"+"'"+cloumn+"'"+","
+"'dataTypeName':"+"'"+dataType+"'"+","
+"'isKey':"+isKey+","
+"'precision':"+precision+","
+"'defaultValue':"+"'"+defaultValue+"'"+","
+"'isNull':"+isNull+","
+"'isAutoIncrement':"+autoIncrement+","
+"'scale':"+scale+"}"+",");
sb.deleteCharAt(sb.length()-1);
sb.append("]");
sb.append("}"); //json字符串到此完成
System.out.println(sb.toString());
tableInfoObj = new JSONObject(sb.toString()); //将json字符串转化为jsonObject对象
return tableInfoObj;
01 背包
void bag01(int cost,int weight) { for(i=v;i>=cost;i--) if(dp[i]<dp[i-cost]+weight) dp[i]=dp[i-cost]+weight; }
完全背包
void complete(int cost,int weight) { for(i=cost;i<=v;i++) if(dp[i]<dp[i-cost]+weight) dp[i]=dp[i-cost]+weight; }
多重背包
void multiply(int cost,int weight,int amount) { if(cost*amount>=v) complete(cost,weight); else{ k=1; while(k<amount){ bag01(k*cost,k*weight); amount-=k; k+=k; } bag01(cost*amount,weight*amount); } }