zoukankan      html  css  js  c++  java
  • LETTers比赛第七场 Guess the Numbers

    这是其它国家区预赛的题目,大家可以看出来区域现场赛最简单题的难度。

    解题的思路很容易想到,四则运算处理可能比较麻烦~但是经过细心调试应该问题不大;还有就是排列的生成可以自己写,但是这里提倡用next_permutation()函数!还有一点是要注意结束的判断。

             以下是参考解题程序:

    View Code
      1 #include<stdio.h>
      2 #include<stack>
      3 #include<algorithm>
      4 #include<ctype.h>
      5 #include<string.h>
      6 using namespace std;
      7 const int maxn=6;
      8 int a[maxn];
      9 char s[1000];
     10 int n;
     11 typedef struct
     12 {
     13     char name[5];
     14     int index;
     15 }unkown;
     16 unkown u[5];
     17 int solve()
     18 {
     19     int i;
     20     sort(a,a+n);
     21     int tot;
     22     int len=strlen(s);
     23     do
     24     {
     25         tot=0;
     26         stack<char>op;
     27         stack<int>value;
     28         for(i=0;i<len;i++)
     29         {
     30             int yes=0;
     31             char name[4];
     32             int tt=0;
     33             while(isalpha(s[i]))
     34             {
     35                 name[tt++]=s[i++];
     36                 yes=1;
     37             }
     38             if(yes)
     39             {
     40                 i--;
     41                 name[tt]=0;
     42                 int j;
     43                 for(j=0;j<tot;j++)if(strcmp(u[j].name,name)==0)
     44                     break;
     45                 if(j==tot){strcpy(u[tot].name,name);u[tot].index=tot;tot++;}
     46                 if(yes)value.push(a[u[j].index]);
     47             }
     48             else  if(s[i]!=')')op.push(s[i]);
     49             else 
     50             {
     51                 char s=op.top();
     52                 op.pop();
     53                 if(s=='*')
     54                 {
     55                     int t,r;
     56                     t=value.top();
     57                     value.pop();
     58                     r=value.top();
     59                     value.pop();
     60                     value.push(t*r);
     61                 }
     62                 else if(s=='+')
     63                 {
     64                     int t,r;
     65                     t=value.top();
     66                     value.pop();
     67                     r=value.top();
     68                     value.pop();
     69                     value.push(t+r);                    
     70                 }
     71                 else if(s=='-')
     72                 {
     73                     int t,r;
     74                     t=value.top();
     75                     value.pop();
     76                     r=value.top();
     77                     value.pop();
     78                     value.push(r-t);
     79                 }
     80                 op.pop();
     81             }
     82         }
     83         if(value.top()==a[n])
     84         { value.pop();return 1;}
     85         value.pop();
     86     }while(next_permutation(a,a+n));
     87     return 0;
     88 }
     89 int main()
     90 {
     91     while(gets(s)!=NULL&&(strstr(s,"0 0")-s)!=0)
     92     {
     93          n=0;
     94         int len=strlen(s);
     95         int i;
     96         sscanf(s,"%d",&n);
     97         i=0;
     98         while(isdigit(s[i])){i++;}
     99         int j;
    100         int tot=0;
    101         for(j=i;j<len;j++)
    102         {
    103             int temp=0;
    104             int yes=0;
    105             while(isdigit(s[j]))
    106             {
    107                 yes=1;
    108                 temp=temp*10+s[j]-'0';
    109                 j++;
    110             }
    111             if(yes)a[tot++]=temp;
    112         }
    113         gets(s);
    114         if(solve())printf("YES\n");
    115         else printf("NO\n");
    116     }
    117     return 0;
    118 }
  • 相关阅读:
    hdu 1116 Play on Words
    hdu 1856 More is better
    跟随鼠标跑
    asp.net实现数据流文件下载
    在ASP.NET程序中集成更好的下载体验
    request.ContentType的可取值
    multipart formdata boundary 说明
    ASP.NET中实现多文件上传(普通)
    读取XML文件中的某个节点的某个属性
    获取请求的Headers部分
  • 原文地址:https://www.cnblogs.com/LETTers/p/2470537.html
Copyright © 2011-2022 走看看