View Code
#include<iostream>
#include<iomanip>
#include<stdio.h>
using namespace std;
//PB,PS,Z,NS,NB分别为0,1,2,3,4
double minu(double a,double b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
double e[5][9]={
{0,0,0,0,0,0,0,0.5,1},
{0,0,0,0,0,0.5,1,0.5,0},
{0,0,0,0.5,1,0.5,0,0,0},
{0,0.5,1,0.5,0,0,0,0,0},
{1,0.5,0,0,0,0,0,0,0}};
double ec[5][5]={
{0,0,0,0,1},
{0,0,0,1,0},
{0,0,1,0,0},
{0,1,0,0,0},
{1,0,0,0,0}};
double u[5][7]={
{0,0,0,0,0,0.5,1},
{0,0,0,0,1,0.5,0},
{0,0,0.5,1,0.5,0,0},
{0,1,0.5,0,0,0,0},
{1,0.5,0,0,0,0,0}};
int rule[5][5]={
{4,4,3,2,0},
{4,4,3,1,0},
{4,3,2,1,0},
{4,3,1,0,0},
{4,2,1,0,0}};
double z[9][5]={0},f[2][7]={0};
double a,b,c[7]={0};
int i,j,x,y,m[2]={0},k,n,l,sign;//m,n指规则
for(x=0;x<9;x++){
for(y=0;y<5;y++){
k=0;
sign=0;
for(i=0;i<2;i++)
for(j=0;j<7;j++)
f[i][j]=0;
for(i=0;i<2;i++)m[i]=0;
for(i=0;i<7;i++)c[i]=0;
for(i=0;i<5;i++){
if(e[i][x]!=0){
m[k]=i;k++;sign++;
}
}
for(j=0;j<5;j++){
if(ec[j][y]!=0){
n=j;
}
}//记录e和ec不为零的项
//规则
for(k=0;k<2;k++){//最多两个规则
for(l=0;l<7;l++){
if(u[rule[m[k]][n]][l]!=0){
f[k][l]=minu(e[m[k]][x],u[rule[m[k]][n]][l]);
}
}
if(sign==1)break;
}
//两次f数组取并
for(l=0;l<7;l++){
if(f[0][l]>f[1][l])
c[l]=f[0][l];
else
c[l]=f[1][l];
}
//计算z
a=0;b=0;
for(l=0;l<7;l++){
a=a+(l-3)*c[l];
b=b+c[l];
}
z[x][y]=(double)a/b;
if(abs(z[x][y])-abs((int)z[x][y])>0.5)
z[x][y]=abs((int)z[x][y])+1;
else
z[x][y]=abs((int)z[x][y]);
if((double)a/b<0 && (int)z[x][y]!=0)
z[x][y]=-z[x][y];
}
}
for(i=0;i<9;i++){
for(j=0;j<5;j++){
printf("%5.0f",z[i][j]);
}
cout<<endl;
}
return 0;
}