ACM 计算器 类似24点 UVALive 6163 Myth Busters

来源:互联网 发布:温州广电网络 编辑:程序博客网 时间:2024/05/04 03:01

ACM 计算器 类似24点 UVALive 6163 Myth Busters


C - Myth Busters
Time Limit:15000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
 UVALive 6163

Description

Download as PDF

Every train carriage operated by CityRail of Sydney has a unique ID number of four digits. A not so uncommon myth amongst local school pupils is that every ID number can be manipulated by permuting the digits, using brackets and using arithmetic operations from the set '*' , '/', '+' , '-' to calculate the number 10.

Your task is to check the validity of this myth for the carriages operated by CityRail of Sydney and for train carriages from other cities whose ID numbers were collected.


Reminder: The operation `/' refers to integer division. Most of you already know it, but here are two examples: result of 5/2 is 2 and of2/5 is 0.

Input

The input consists of many test cases. The description of each test case consists of:

  • an integer N (1$ \le$N$ \le$1000), on a line by itself, which indicates the number of IDs collected from one city, and
  • N lines that contain a four-digit number each.

A zero on a line by itself indicates the end of input and should not be processed.

Output

For each test case print the conclusion of your investigation as `TRUE' or `BUSTED' as shown in ``Sample Output" below. Print `TRUE' if this myth is correct for all carriage ID numbers for that city. Otherwise, `BUSTED' is to be printed.

Sample Input

26666555521234161116020

Sample Output

BUSTEDTRUE

Memory: 0 KB Time: 73 MSLanguage: C++ 4.5.3 Result: Accepted

/* * Author: NICK WONG * Created Time:  8/2/2014 13:11:54 * File Name:  */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;#define out(x) cout<<#x<<": "<<x<<endlconst double eps(1e-8);const int maxn=11000;const long long maxint=-1u>>1;const long long maxlong=maxint*maxint;typedef long long lint;int n;int a[maxn][5],p,b[10],ans[maxn];bool vis[10],flag,wjj;void init(){    char c;    for (int i=1; i<=n; i++)    {        for (int j=1; j<=4; j++)        {            cin>>c;            a[i][j]=c-'0';                  }         }            }void dfs(int l, int r,int ans[])//计算排列的值,数组ans存储可能的结果情况,目测最多302种左右{    int x[maxn],y[maxn];    if (l==r)    {        ans[0]=1;        ans[1]=b[l];        return;    }    ans[0]=0;    for (int i=l; i<r; i++)    {        dfs(l,i,x);        dfs(i+1,r,y);        for (int j=1; j<=x[0]; j++)            for (int k=1; k<=y[0]; k++)            {                ans[++ans[0]]=x[j]*y[k];                ans[++ans[0]]=x[j]+y[k];                ans[++ans[0]]=x[j]-y[k];                if (y[k]!=0) ans[++ans[0]]=x[j]/y[k];            }    }}void dg(int x)//生成排列{    if (wjj) return;    if (x==5)     {        ans[0]=0;        dfs(1,4,ans);        for (int i=1; i<=ans[0]; i++)            if (ans[i]==10)             {                wjj=true;                break;            }        //out(ans[0]);        return;    }    for (int i=1; i<=4; i++)        if (!vis[i])        {            vis[i]=true;            b[x]=a[p][i];            dg(x+1);            vis[i]=false;        }}void work(){    flag=true;    memset(vis,false,sizeof(vis));    for (p=1; p<=n; p++)    {        wjj=false;        dg(1);        if (!wjj) flag=false;    }    if (flag) cout<<"TRUE"<<endl; else cout<<"BUSTED"<<endl;}int main(){    while(cin>>n && n!=0)    {        init();        work();    }    return 0;}


0 0
原创粉丝点击