某机器人可以按上下左右移动。。。现给出一串指令,机器人所处空间无限大,问有多少该指令的某一连续子串,使得机器人执行后回原位
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e3+10;
char s[maxm];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
int sum=0;
for(int i=0; i<strlen(s); i++)
{
for(int j=i+1; j<strlen(s); j++)
{
int x=0,y=0;
for(int k1=i; k1<=j; k1++)
{
if(s[k1]=='U')
{
y++;
}
if(s[k1]=='R')
{
x++;
}
if(s[k1]=='L')
{
x--;
}
if(s[k1]=='D')
{
y--;
}
}
if(x==0&&y==0)
{
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}
Catherine有n张卡片,每张卡片分别是蓝绿红的其中一种颜色每次可以执行两种操作中的一种:
1:将两张同色卡片换成一张该颜色卡
2:将两张异色卡片换成一张第三种颜色卡
问:最后剩的一张卡可能是什么颜色
所有情况都讨论一遍就好。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e3+10;
char s[maxm];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
int b=0,r=0,g=0;
for(int i=0;i<n;i++)
{
if(s[i]=='B')
b++;
else if(s[i]=='R')
r++;
else
g++;
}
if(b&&g&&r)
{
printf("BGR\n");
continue;
}
if(!b&&!g)
{
printf("R\n");
continue;
}
if(!b&&!r)
{
printf("G\n");
continue;
}
if(!r&&!g)
{
printf("B\n");
continue;
}
if(!b)
{
if(g>1&&r>1)
{
printf("BGR\n");
continue;
}
else if(g==1&&r==1)
{
printf("B\n");
continue;
}
else if(g==1)
{
printf("BG\n");
continue;
}
else
{
printf("BR\n");
continue;
}
}
if(!g)
{
if(b>1&&r>1)
{
printf("BGR\n");
continue;
}
else if(b==1&&r==1)
{
printf("G\n");
continue;
}
else if(b==1)
{
printf("BG\n");
continue;
}
else
{
printf("GR\n");
continue;
}
}
if(!r)
{
if(g>1&&b>1)
{
printf("BGR\n");
continue;
}
else if(g==1&&b==1)
{
printf("R\n");
continue;
}
else if(g==1)
{
printf("GR\n");
continue;
}
else
{
printf("BR\n");
continue;
}
}
}
return 0;
}
好多小孩在玩建塔游戏。。。其中一队只有高度为2的积木,另一队只有高度为3的积木,他们都不喜欢自己建的塔的高度和其他小孩一样,问满足条件情况下最高的塔的高度最低是多少
对于任意i,记
a1:小于等于i的所有数中是2的倍数但不是6的倍数的数
a2:小于等于i的所有数中是3的倍数但不是6的倍数的数
a3:小于等于i的所有数中是6的倍数的数
若a1+a2+a3 >= n+m 则i是解
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;;i++)
{
int a1=max(n-(i/2-i/6),0);
int a2=max(m-(i/3-i/6),0);
int a3=i/6;
if(a1+a2<=a3)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}