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

poj 1348 Computing

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

poj 1348 Computing

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct Nod{    int son; //分子    int mon; //分母}num[5];  //以 分子/分母 形式保存一个数void getNum(int *a)  //将a数组转换成 分子/分母 形式{    int i;    for(i=0;i<4;i++)    {        num[i].son = a[i];        num[i].mon = 1;    }}Nod operate(Nod a,Nod b,int ch)  //进行四则运算{    Nod temp;    if(ch==0)  // '+'    {        temp.mon = a.mon * b.mon;        temp.son = a.son * b.mon + b.son * a.mon;    }    else if(ch==1)  // '-'    {        temp.mon = a.mon * b.mon;        temp.son = a.son * b.mon - b.son * a.mon;    }    else if(ch==2)  //  '*'    {        temp.mon = a.mon * b.mon;        temp.son = a.son * b.son;    }    else if(ch==3)  //  '/'    {        temp.mon = a.mon * b.son;        temp.son = b.mon * a.son;    }    return temp;}int computing(int *a,int e){    getNum(a);  //获得  分子/分母 的表示方式    Nod temp1,temp2,temp3;    int i,j,k;    // ((a#b)#c)#d   '#'号代表运算符号    for(i=0;i<4;i++)    {        temp1 = operate(num[0],num[1],i);        if(temp1.mon == 0)  continue;   //分母为0情况        for(j=0;j<4;j++)        { temp2 = operate(temp1,num[2],j); if(temp2.mon == 0) continue; for(k=0;k<4;k++) {      temp3 = operate(temp2,num[3],k);      if(temp3.mon == 0) continue;      if(temp3.son%temp3.mon==0&&temp3.son/temp3.mon==e) return 1; }        }    }    //(a#(b#(c#d)))    for(i=0;i<4;i++)    {        temp1 = operate(num[2],num[3],i);        if(temp1.mon == 0)  continue;        for(j=0;j<4;j++)        { temp2 = operate(num[1],temp1,j); if(temp2.mon == 0) continue; for(k=0;k<4;k++) {      temp3 = operate(num[0],temp2,k);      if(temp3.mon == 0) continue;      if(temp3.son%temp3.mon==0&&temp3.son/temp3.mon==e) return 1; }        }    }    //(a#b)#(c#d)    for(i=0;i<4;i++)    {        temp1 = operate(num[0],num[1],i);        if(temp1.mon == 0)  continue;        for(j=0;j<4;j++)        { temp2 = operate(num[2],num[3],j); if(temp2.mon == 0) continue; for(k=0;k<4;k++) {      temp3 = operate(temp1,temp2,k);      if(temp3.mon == 0) continue;      if(temp3.son%temp3.mon==0&&temp3.son/temp3.mon==e) return 1; }        }    }    return 0;}int main(){    int a[5],e;    while(~scanf("%d",&a[0])&&a[0]!=-1)    {        scanf("%d%d%d%d",&a[1],&a[2],&a[3],&e);        int i,j;        for(j=0;j<4;j++) printf("%d ",a[j]);        for(i=0;i<24;i++)        { if(computing(a,e) == 1) break; next_permutation(a,a+4);   //获取下一个字典序        }        printf("%d ",e);        if(i<24) puts("OK!");        else puts("NO!");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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