栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1487 Single

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1487 Single

#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <cmath>using namespace std;#define print(x) cout<<x<<endl#define input(x) cin>>x#define MAT 32#define SIZE 1024#define pb push_backconst double eps=1e-8;inline bool zero(double x){return fabs(x)<eps;}struct node{    int type;    int val;    node(){}    node(int i_type,int i_val)    {        type=i_type;        val=i_val;    }};vector<int> g[SIZE];double mat[MAT][MAT];node array[SIZE];int n;int ind;char *p;void build(int now){    int t,m;    while(*p)    {        while(*p==' ' && *p) p++;        if(!*p) break;        if( (*p>='0' && *p<='9') || *p=='-')        { ind++; sscanf(p,"%d%n",&t,&m); p+=m; array[ind]=node(0,t); g[now].pb(ind);        }        else if(*p=='(')        { ind++; array[ind]=node(-1,0); p++; g[now].pb(ind); build(ind);        }        else if(*p==')')        { p++; return;        }        else        { ind++; array[ind]=node(1,*p-'a'); p++; g[now].pb(ind);        }    }}void toMatrix(int now,double tp,int var){    double p;    if(g[now].size()) p=tp/g[now].size();    //写入Matrix的基本原理:    //例如a = (2 , b)    //a的E(a)期望等于 1/2 * 2 + 1/2 * E(b)    //所以,对于Mat[ord(a)]来说,即为:    //      1*E(A) -1/2*E(B) = 1/2*2    for(int i=0;i<(int)g[now].size();i++)    {        int x=g[now][i];        if(array[x].type==-1)        { toMatrix(x,p,var);        }        else        { if(array[x].type==0) {     mat[var][n]+=array[x].val*p; } else {     mat[var][array[x].val]-=p; }        }    }}void init(){    ind=0;    memset(array,0,sizeof(array));    for(int i=0;i<SIZE;i++) g[i].clear();}void gauss_line(int a,int b,int col){    double mul_a=mat[a][col];    double mul_b=mat[b][col];    for(int i=0;i<=n;i++)    {        mat[b][i]=mat[b][i]-mat[a][i]*mul_b/mul_a;    }}void gauss(){    for(int row=0,col=0;row<n && col<n;row++,col++)    {        int ptr=-1;        for(int i=row;i<n;i++)        { if(!zero(mat[i][col])) {     ptr=i;     break; }        }        if(ptr==-1) continue;        else        { for(int i=0;i<=n;i++) {     swap(mat[row][i],mat[ptr][i]); } for(int i=0;i<n;i++) if(i!=row) {     gauss_line(row,i,col); }        }    }}double getans(int x){    return mat[x][n]/mat[x][x];}bool check_exist(int x){    for(int i=0;i<n;i++) if(i!=x)    {        if(!zero(mat[x][i])) return false;    }    return !zero(mat[x][x]);}int main(){    int cas=1;    char cmd[SIZE];    while(input(n) && n)    {        memset(mat,0,sizeof(mat));        printf("Game %dn",cas++);        for(int i=0;i<n;i++)        { init(); do {     gets(cmd); }while(*cmd==''); p=cmd; while(*p!='(') p++; //p++; build(0); toMatrix(0,1.,i); mat[i][i]+=1.0;        }        gauss();        for(int i=0;i<n;i++)        { if(check_exist(i)) printf("Expected score for %c = %.3fn",i+'a',getans(i)); else printf("Expected score for %c undefinedn",i+'a');        }        puts("");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号