3295
题目描述
就是给了你五种计算方式,都是以前缀的形式给你的,即最开始的字符是操作类型,后面是操作数,其实这稍微想一下就和后缀表达式道理是一样的,只不过是换成了前缀。五种操作分别是:与,或,非,前一个数的非或后一个数,同或。
然后问你这个式子是不是永真式。因为五个参数,最后的结果最多也就是32个,我们直接遍历,看是不是都为1。
代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <string.h>
using namespace std;
typedef long long ll;
char str[200];
int a[50][50]= {0};
int qiu1(char ch,int p,int q,int r,int s,int t)
{
if(ch=='p')
return p;
else if(ch=='q')
return q;
else if(ch=='r')
return r;
else if(ch=='s')
return s;
else
return t;
}
int qiu2(char ch,int zhi1,int zhi2)
{
if(ch=='K')
return zhi1&zhi2;
else if(ch=='A')
return zhi1|zhi2;
else if(ch=='C')
{
if(zhi1==1)
return 0|zhi2;
else
return 1|zhi2;
}
else
return zhi1==zhi2;
}
int qiu3(int zhi)
{
if(zhi==0)
return 1;
else
return 0;
}
bool judge(int p,int q,int r,int s,int t,int len)
{
stack<int> sta;
for(int i=len-1;i>=0;i--)
{
if(str[i]>='a'&&str[i]<='z')
sta.push(qiu1(str[i],p,q,r,s,t));
else
{
if(str[i]=='N')
{
int zhi=sta.top();
sta.pop();
sta.push(qiu3(zhi));
}
else
{
int zhi1=sta.top();
sta.pop();
int zhi2=sta.top();
sta.pop();
sta.push(qiu2(str[i],zhi1,zhi2));
}
}
}
int k=sta.top();
if(k==0)
return false;
else
return true;
}
void solve()
{
while(true)
{
scanf("%s",str);
if(str[0]=='0')
break;
int len=strlen(str);
bool biao=true;
for(int i=0; i<32; i++)
{
if(judge(a[i][1],a[i][2],a[i][3],a[i][4],a[i][5],len)==false)
{
biao=false;
break;
}
}
if(biao)
printf("tautology\n");
else
printf("not\n");
}
}
int main()
{
int t=1;
for(int i=0; i<32; i++)
{
int k=i;
for(int j=5; j>=1; j--)
{
a[i][j]=k&1;
k>>=1;
}
}
while(t--)
{
solve();
}
return 0;
}
1068
题目描述
给定你一个由n个数组成的序列,这个序列为
p
p
p,其中
p
i
p_i
pi的含义为第i个右括号左边的左括号数量,而要求的是下面一个由n个数组成的序列
w
w
w:
w
i
w_i
wi表示第
i
i
i个右括号和与其匹配的左括号之间所有匹配后的括号个数。
至于怎么算,就是下面的一个规律了,可以自己举几个例子试一下,规律还是比较简单的,可以从代码中看出来。
代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <string.h>
using namespace std;
typedef long long ll;
int a[50];
void solve()
{
int n;
scanf("%d",&n);
a[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
int j=i-1;
while(a[i]-a[j]<i-j)
j--;
printf("%d ",i-j);
}
printf("\n");
}
int main()
{
int t=1;
scanf("%d",&t);
while(t--)
{
solve();
}
return 0;
}
2632
题目大意
这道题目就是一个纯模拟题,不过里面还是有一些坑,最主要的就是这个坐标系,你需要先将相应的坐标转换成数组的下标,这两者之间不能搞混,还有就是题目里面的方向一定要严格按照题目下面的那个图里面的坐标方向来写,不然是一定会错的。
代码
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#define inf 0x3f3f3f3f
#define mod 1000000007
#define lmax 1000
#define pi 3.141592653589793238462643383279
#define eps 1e-5
using namespace std;
typedef long long ll;
typedef double type;
const int maxn=150;
int a,b,n,m;
struct robot{//三个值分别表示当前机器人的x,y坐标和当前的方向
int x,y;
int value;
}ro[maxn];
bool mark=true;
int pan(char ch)
{
if(ch=='N')
return 0;
else if(ch=='W')
return 1;
else if(ch=='S')
return 2;
else
return 3;
}
void judge(int x,int y,int i)
{
if(x==0||x==b+1||y==0||y==a+1)
{
printf("Robot %d crashes into the wall\n",i);
mark=false;
}
else
{
int d=0;
for(int j=1;j<=n;j++)
{
if(j==i) continue;
else if(ro[j].x==x&&ro[j].y==y)
{
d=j;
break;
}
}
if(d!=0)
{
printf("Robot %d crashes into robot %d\n",i,d);
mark=false;
}
}
}
void turn(int no,char ch,int repeat)
{
if(!mark)
return ;
if(ch=='L')
ro[no].value=(ro[no].value+repeat+400)%4;
else if(ch=='R')
ro[no].value=(ro[no].value-repeat+400)%4;
else
{
for(int i=1;i<=repeat;i++)
{
if(ro[no].value==0)
ro[no].x--;
else if(ro[no].value==2)
ro[no].x++;
else if(ro[no].value==1)
ro[no].y--;
else if(ro[no].value==3)
ro[no].y++;
judge(ro[no].x,ro[no].y,no);
if(!mark)
break;
}
}
}
void solve()
{
scanf("%d%d",&a,&b);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int x,y;
char d;
scanf("%d%d %c",&x,&y,&d);
ro[i].x=b+1-y;
ro[i].y=x;
ro[i].value=pan(d);
}
mark=true;
for(int k=1;k<=m;k++)
{
int no,repeat;
char d;
scanf("%d %c%d",&no,&d,&repeat);
if(mark==false)
continue;
turn(no,d,repeat);
}
if(mark)
printf("OK\n");
}
int main()
{
int t=1;
scanf("%d",&t);
while(t--)
{
solve();
}
return 0;
}
1573
题目大意
这道题目就是一个纯模拟题,题意就是操控一个小人从迷宫的第一行走,开始元素告诉你了,然年每一个元素都有一个方向,小人走到这个元素之后只能向这个方向走,最后问小人是走到了迷宫外面还是走进了一个死循环。基本上跟着题意模拟就行了,而且迷宫的范围也不大。
代码
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#define inf 0x3f3f3f3f
#define mod 1000000007
#define lmax 1000
#define pi 3.141592653589793238462643383279
#define eps 1e-5
using namespace std;
typedef long long ll;
typedef double type;
const int maxn=150;
char ma[maxn][maxn];
int ma2[maxn][maxn];
void solve()
{
while(true)
{
int x,y,start;
scanf("%d%d%d",&x,&y,&start);
if(x==0&&y==0&&start==0)
break;
for(int i=0;i<x;i++)
{
scanf("%s",ma[i]);
}
memset(ma2,0,sizeof(ma2));
int startx=0,starty=start-1;
int cnt=1;
while(true)
{
if(startx<0||startx>=x||starty<0||starty>=y)
{
printf("%d step(s) to exit\n",cnt-1);
break;
}
if(ma2[startx][starty]!=0)
{
printf("%d step(s) before a loop of %d step(s)\n",ma2[startx][starty]-1,cnt-ma2[startx][starty]);
break;
}
if(ma[startx][starty]=='N')ma2[startx][starty]=cnt,startx--;
else if(ma[startx][starty]=='S')ma2[startx][starty]=cnt,startx++;
else if(ma[startx][starty]=='W')ma2[startx][starty]=cnt,starty--;
else if(ma[startx][starty]=='E')ma2[startx][starty]=cnt,starty++;
cnt++;
}
}
}
int main()
{
int t=1;
while(t--)
{
solve();
}
return 0;
}