Archive for March, 2007
wow的单机版玩法
knapsack problem
Posted by xiaokucha in Programming on March 14, 2007
From Gossip@caterpillar
Algorithm Gossip: 背包問題(Knapsack Problem)
說明
假設有一個背包的負重最多可達8公斤,而希望在背包中裝入負重範圍內可得之總價物品,假設是水果好了,水果的編號、單價與重量如下所示:
0 | 李子 | 4KG | NT$4500 |
1 | 蘋果 | 5KG | NT$5700 |
2 | 橘子 | 2KG | NT$2250 |
3 | 草莓 | 1KG | NT$1100 |
4 | 甜瓜 | 6KG | NT$6700 |
解法
背包問題是關於最佳化的問題,要解最佳化問題可以使用「動態規劃」(Dynamic programming),從空集合開始,每增加一個元素就先求出該階段的最佳解,直到所有的元素加入至集合中,最後得到的就是最佳解。
以背包問題為例,我們使用兩個陣列value與item,value表示目前的最佳解所得之總價,item表示最後一個放至背包的水果,假設有負重量 1~8的背包8個,並對每個背包求其最佳解。
逐步將水果放入背包中,並求該階段的最佳解:
- 放入李子
背包負重 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
value | 0 | 0 | 0 | 4500 | 4500 | 4500 | 4500 | 9000 |
item | - | - | - | 0 | 0 | 0 | 0 | 0 |
- 放入蘋果
背包負重 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
value | 0 | 0 | 0 | 4500 | 5700 | 5700 | 5700 | 9000 |
item | - | - | - | 0 | 1 | 1 | 1 | 0 |
- 放入橘子
背包負重 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
value | 0 | 2250 | 2250 | 4500 | 5700 | 6750 | 7950 | 9000 |
item | - | 2 | 2 | 0 | 1 | 2 | 2 | 0 |
- 放入草莓
背包負重 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
value | 1100 | 2250 | 3350 | 4500 | 5700 | 6800 | 7950 | 9050 |
item | 3 | 2 | 3 | 0 | 1 | 3 | 2 | 3 |
- 放入甜瓜
背包負重 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
value | 1100 | 2250 | 3350 | 4500 | 5700 | 6800 | 7950 | 9050 |
item | 3 | 2 | 3 | 0 | 1 | 3 | 2 | 3 |
由最後一個表格,可以得知在背包負重8公斤時,最多可以裝入9050元的水果,而最後一個裝入的水果是3號,也就是草莓,裝入了草莓,背包只能再放入7公斤(8-1)的水果,所以必須看背包負重7公斤時的最佳解,最後一個放入的是2號,也就是橘子,現在背包剩下負重量5公斤(7-2),所以看負重5公斤的最佳解,最後放入的是1號,也就是蘋果,此時背包負重量剩下0公斤(5-5),無法再放入水果,所以求出最佳解為放入草莓、橘子與蘋果,而總價為9050元。
實作
- C
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 8 // 重量限制
#define N 5 // 物品種類
#define MIN 1 // 最小重量
struct body {
char name[20];
int size;
int price;
};
typedef struct body object;
int main(void) {
int item[LIMIT+1] = {0};
int value[LIMIT+1] = {0};
int newvalue, i, s, p;
object a[] = {{"李子", 4, 4500},
{"蘋果", 5, 5700},
{"橘子", 2, 2250},
{"草莓", 1, 1100},
{"甜瓜", 6, 6700}};
for(i = 0; i < N; i++) {
for(s = a[i].size; s <= LIMIT; s++) {
p = s - a[i].size;
newvalue = value[p] + a[i].price;
if(newvalue > value[s]) {// 找到階段最佳解
value[s] = newvalue;
item[s] = i;
}
}
}
printf("物品t價格n");
for(i = LIMIT; i >= MIN; i = i - a[item[i]].size) {
printf("%st%dn",
a[item[i]].name, a[item[i]].price);
}
printf("合計t%dn", value[LIMIT]);
return 0;
}
- Java
class Fruit {
private String name;
private int size;
private int price;
public Fruit(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getSize() {
return size;
}
}
public class Knapsack {
public static void main(String[] args) {
final int MAX = 8;
final int MIN = 1;
int[] item = new int[MAX+1];
int[] value = new int[MAX+1];
Fruit fruits[] = {
new Fruit("李子", 4, 4500),
new Fruit("蘋果", 5, 5700),
new Fruit("橘子", 2, 2250),
new Fruit("草莓", 1, 1100),
new Fruit("甜瓜", 6, 6700)};
for(int i = 0; i < fruits.length; i++) {
for(int s = fruits[i].getSize(); s <= MAX; s++) {
int p = s - fruits[i].getSize();
int newvalue = value[p] +
fruits[i].getPrice();
if(newvalue > value[s]) {// 找到階段最佳解
value[s] = newvalue;
item[s] = i;
}
}
}
System.out.println("物品t價格");
for(int i = MAX;
i >= MIN;
i = i - fruits[item[i]].getSize()) {
System.out.println(fruits[item[i]].getName()+
"t" + fruits[item[i]].getPrice());
}
System.out.println("合計t" + value[MAX]);
}
}
APC(a perfect circle) Recommended
Alternative Metal(另类金属) Alternative Pop/Rock(另类流行/摇滚) Post-Grunge(后垃圾)
ps:个人比较崇拜MJK(Maynard James Keenan)的歌唱功力。。。平静而富有杀伤力,dark silence sad metal,比较喜欢专辑里面的Orestes,Judith,the outsider等。。。
DataStructure chapter 1
Posted by xiaokucha in Programming on March 13, 2007
{
LinkList *p=La;int j=0;
while(p&&j<i-1)
{p=p->next;++j;}
if(!p||j>i-1)
return 1;
else
{
s= new LNode;//s=(LinkList)malloc(sizeof(LNode));
s->data=b;
s->next=p->next;
p->next=s;
return 0;
}
}
{
LinkList *p=La;int j=0; LinkList *q;
while(p&&j<i-1)
{p=p->next;++j;}
if(!(p->next)||j>i-1)
return 1;
else
{
q=p->next;
p->next=q->next;
b=q->data;
delete q;
return 0:
}
}
{
la=new LNode;la->next=NULL;
for(i=n;i>=1;i–)
{
p=new LNode;
p->data=a[i];
p->next=la->next;
la->next=p;
}
}
{
ap=a->next;bp=b->next;tp=a;
while(ap)
{
if(!bp||ap->data<bp->data)
{
tp->next=ap->next;
delete ap;
ap=tp->next;}
else if(ap->data=bp->data)
{
tp=ap;
ap=ap->next;
bp=bp->next;
}
else bp=bp->next;
}
}
{
c= new LNode;
ap=a->next;bp=b->next;cp=c;
while(ap&&bp)
{
if(ap->data=bp->data)
{
tp=new LNode;
tp->data=ap->data;
cp->next=tp;cp=tp;
ap=ap->next;
bp=bp->next;
}
else if(ap->data<bp->data)
ap=ap->data;
else
bp=bp->data;
}
cp->next=NULL;
}
{
ap=a->next;bp=b->next;tp=a;
while(ap&&bp)
{
if(!bp||ap->data<bp->data)
{
ap=ap->next;
tp=tp->next;
}
else if(ap->data==bp->data)
{
tp->next=ap->next;
delete ap;
ap=tp->next;
}
else
bp=bp->next;
}
}
{
c=new LNode;
ap=a->next;
bp=b->next;
cp=c;
while(ap)
{
if(!bp)
{
tp=new LNode;
cp->next=tp;cp=tp;cp->data=ap->data;ap=ap->next;
}
else if(ap->data<bp->data){
tp=new LNode;cp->next=tp;cp=tp;cp->data=ap->data;ap=ap->next;
}
else if(ap->data==bp->data){
ap=ap->next;bp=bp->next;
}
else bp=bp->next;
}
cp->next=NULL;
}
{
ap=a->next;bp=b->next;tp=a;
while(ap&&bp)
{
if(ap->data<bp->data)
{tp->next=ap;tp=ap;ap=ap->next;}
else if(ap->data>bp->data)
{tp->next=bp;tp=bp;bp=bp->next;}
else{
tp->next=ap;tp=bp;bp=bp->next;
delete tp;
tp=ap;ap=ap->next;}
}
if(ap)tp->next=ap;
else tp->next=bp;
delete b;
}
{
c=new LNode;
ap=a->next;bp=b->next;cp=c;
while(ap||bp)
{
tp=new LNode;
cp->next=tp;
cp=tp;
if(!ap)
{cp->data=bp->data;bp=bp->next;}
else if(!bp)
{cp->data=ap->data;ap=ap->next;}
else if(ap->data==bp->data)
{cp->data=ap->data;ap=ap->next;bp=bp->next;}
else if(ap->data<bp->data)
{cp->data=ap->data;ap=ap->data;}
else
{cp->data=bp->data;bp=bp->data;}
cp->next=NULL;
}
根据preorder和inorder显示tree
Posted by xiaokucha in Programming on March 1, 2007
the input length is:7
plz input the in sequence:cbedafg
0 0 7 4 1 0 0
1 0 4 4 2 7 1
2 0 1 4 3 4 1
3 0 0 4 4 2 1
3 1 0 4 4 2 2
3 2 2 4 3 4 2
4 2 1 4 4 6 1
5 2 0 4 5 5 1
5 3 0 4 5 5 2
5 4 0 4 4 6 2
5 5 2 4 2 7 2
6 5 0 4 3 10 1
6 6 1 4 3 10 2
7 6 0 4 4 12 1
7 7 0 4 4 12 2
a
b f
c d g
e
#include <stdlib.h>
#include <math.h>
char treepre[10],treein[10];
char output[50][50];
getlv(char *s,char *t,int a,int b,int len){
//printf("%d %d %dn",a,b,len);
int i=1,n,m;
if(len==0)
return 0;
else{
while((s[a]!=t[b+i-1])&&i<len)
i++;
n=1+getlv(s,t,a+1,b,i-1);
m=1+getlv(s,t,a+i,b+i,len-i);
return (n>m)?n:m;
}
}
void prein(char *s,char *t,int a,int b,int len,int lv,int lvn,int pos,int lorr)
{
printf("%d %d %d %d %d %d %dn",a,b,len,lv,lvn,pos,lorr);
if(lorr==0){
pos=(pow(2,lv)-1)/2;
//printf("%dn",pos);
}
else if(lorr==1)
pos=pos-(lv-lvn+1);
else if(lorr==2)
pos=pos+(lv-lvn+1);
int i=1;
if(len!=0)
{
while((s[a]!=t[b+i-1])&&i<len)
i++;
output[lvn-1][pos]=s[a];
lvn++;
prein(s,t,a+1,b,i-1,lv,lvn,pos,1);
prein(s,t,a+i,b+i,len-i,lv,lvn,pos,2);
}
}
int main()
{
int n,m,lv=1,len;
for(n=0;n<50;n++)
for(m=0;m<50;m++)
output[n][m]=’ ‘;
pp=ip=0;
printf("please input the pre sequence:");
scanf("%s",treepre);
// printf("%s",sf);
len=n=strlen(treepre);
printf("the input length is:%d",n);
printf("nplz input the in sequence:");
scanf("%s",treein);
m=strlen(treein);
if(m!=n){printf("error: pre not equal to in");exit(0);}
//while((n=n/2)!=0)
//lv++;
lv=getlv(treepre,treein,0,0,len);
printf("%dn",lv);
prein(treepre,treein,0,0,len,lv,1,0,0);
for(n=0;n<50;n++){
for(m=0;m<50;m++){
printf("%c",output[n][m]);}
printf("n");}
// printf("%s",sm);
system("PAUSE");
return 0;
}
calculator
Posted by xiaokucha in Programming on March 1, 2007
#include <stdlib.h>
#include <ctype.h>
#define NUMBER 0
#define BUFSIZE 100
#define MAXVAL 100
int sp_op=0;
char buf[BUFSIZE];
int bufp=0;
double OPND[MAXVAL];
char OPTR[MAXOP];
{
if(sp_nd<MAXVAL)
OPND[sp_nd++]=f;
else
printf("error: nd stack full,can’t push %gn",f);
}
void pushop(char w)
{
if(sp_op<MAXOP)
OPTR[sp_op++]=w;
else
printf("error: op stack full,can’t push %cn",w);
}
{
double a;
if(sp_nd>0)
{a=OPND[–sp_nd];
OPND[sp_nd]=”;
return a;}
else{
printf("error: nd stack emptyn");
return 0;
}
}
char popop(void)
{
char a;
if(sp_op>0){
a=OPTR[–sp_op];
OPTR[sp_op]=”;
return a;}
else{
printf("error: op stack emptyn");
return 0;
}
}
{
return (bufp>0)?buf[–bufp]:getchar();
}
{
if(bufp>=BUFSIZE)
printf("ungetch:too many charactersn");
else
buf[bufp++]=c;
}
{
double val,power;
int exp,i,sign;
for (i=0;isspace(s[i]);i++)
;
sign = (s[i]==’-‘)?-1:1;
if(s[i]==’+’||s[i]==’-‘)
i++;
for(val=0.0;isdigit(s[i]);i++)
val=10.0*val+(s[i]-‘0’);
if(s[i]==’.’)
i++;
for(power=1.0;isdigit(s[i]);i++)
{
val=10.0*val+(s[i]-‘0’);
power*=10.0;
}
val=sign*val/power;
if(s[i]==’e’||s[i]==’E’){
sign=(s[++i]==’-‘)?-1:1;
if(s[i]==’+’||s[i]==’-‘)
i++;
for(exp=0;isdigit(s[i]);i++)
exp=10*exp+(s[i]-‘0’);
if(sign==1)
while(exp–>0)
val*=10;
else
while(exp–>0)
val/=10;
}
return val;
}*/
getop(char s[])
{
int i,c;
while((s[0]=c=getch())==’ ‘|| c==’t’)
;
s[1]=”;
i=0;
if(!isdigit(c)&&c!=’.’&&c!=’-‘)
return c;
if(c==’-‘)return ‘-‘;
//c==getch();
/*if(c==’-‘){
if(isdigit(c==getch())||c==’.’)
s[++i]=c;
else{
if(c!=EOF)
ungetch(c);
return ‘-‘;
}*/
if(isdigit(c))
while (isdigit(s[++i]= c =getch()))
;
if(c==’.’)
while(isdigit(s[++i]=c=getch()))
;
s[i]=”;
if(c != EOF)
ungetch(c);
// printf("%sn",s);
return NUMBER;
}
double result;
printf("%cn",theta);
switch(theta){
case ‘+’:result=a+b;break;
case ‘-‘:result=a-b;break;
case ‘*’:result=a*b;break;
case ‘/’:result=a/b;break;
default:
printf("op resolving error: %cn",theta);
}
printf("operate result:%gn",result);
return result;
}
printf("%c,%cn",a,b);
printf("%dn",sp_op);
if(a==’)’&&b!='(‘)
return ‘<‘;
if(a=='(‘)
return ‘>’;
if((a==’)’)&&(b=='(‘))
return ‘=’;
if(a==’#’)
return ‘<‘;
if(b==’#’)
return ‘>’;
if((a==’+’)||(a==’-‘))
{
if(b=='(‘)
return ‘>’;
else
return ‘<‘;
}
if((a==’*’)||(a==’/’))
{
if(b==’*’||b==’/’||b=='(‘)
return ‘<‘;
else if(b==’+’||b==’-‘||b==’)’)
return ‘>’;
}
int
main()
{
double a,b;
char x,theta;
int c,d;
int type=0;
char w[MAXOP];
memset(w,0,sizeof(char));
pushop(‘#’);
type=getop(w);
while(!((type==’#’)&&OPTR[sp_op-1]==’#’))
if(!type)
{printf("%dn",type);pushnd(atof(w));
c=d=0;
while(OPND[c]!=0)
printf("%gn",OPND[c++]);
while(OPTR[d]!=”)
printf("%cn",OPTR[d++]);
system("pause");
type=getop(w);
printf("%dn",type);printf("get op: %sn",w);
system("pause");}
else {
printf("%cn",Prec(type,OPTR[sp_op-1]));
switch (Prec(type,OPTR[sp_op-1])){
case ‘>’: pushop(type);type=getop(w);printf("get op: %sn",w);break;
case ‘=’: x=popop();type=getop(w);printf("get op: %sn",w);break;
case ‘<‘: theta = popop();
b=popnd();a=popnd();
pushnd(operate(a,theta,b));
break;
}
}
x=popop();
printf("nresult is %g",popnd());
system("pause");
return 0;
}