题目
题目意思是每行输入两个筛子,然后判断两个筛子的是不是用了相同的喷涂方式,即两个筛子是否为同一个(忽略数字)。每个筛子最多有三种颜色r、g、b。每行输入12个字符不含空格。
我们可以想想,如果我们人来操作是怎么样做的?
第一步是把同色的一面对着自己。(此时视为顶部,编号1)
第二步是看底部是否同色。(底部编号6)
第三步是看能否通过水平旋转使得完全相同。
想清楚之后就可以敲代码了。值得注意的是输入的处理,笔者这里用了一个函数read()来读入,也增加一点抗性。还有使用string里面的函数时要确保字符串有结束符。
代码
#include <stdio.h>
#include <string.h>
const int maxn = 6;
char c1[maxn + 2], c2[maxn + 2], ct[maxn + 2];
int read();
void find(char s[], char aid, int index[]);
void turn(char s[], int i);
void turnH(char s[]);
void turnV(char s[]);
int main() {
#ifdef TEST
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif // TEST
while (read()) {
int flag = 0;
int index[maxn + 2];
find(c1, c2[1], index);
for (int i = 0; i < 6 && index[i]; i++) {
strcpy(ct, c1); turn(ct, index[i]);
if (ct[6] != c2[6]) continue;
for (int j = 0; j < 4 && strcmp(ct, c2); j++) turnH(ct);
if (!strcmp(ct, c2)) flag = 1;
}
if (flag) printf("TRUE\n");
else printf("FALSE\n");
}
return 0;
}
int read(){
//因为会用到大量的string里的函数,所以需要确保有结束符。
memset(c1, ' ', sizeof(c1)); c1[7] = '\0';
memset(c2, ' ', sizeof(c2)); c2[7] = '\0';
char ch = getchar();
//最有可能读到的不想要的字符就是这两个
while (ch == '\n' || ch == ' ') ch = getchar();
if (ch == EOF) return 0;
c1[1] = ch;
for (int i = 2; i <= maxn; i++) c1[i] = getchar();
for (int i = 1; i <= maxn; i++) c2[i] = getchar();
return 1;
}
void find(char s[], char aid, int index[]){
memset(index, 0, sizeof(index));
for (int i = 1, n = 0; i <= 6; i++)
if (s[i] == aid) index[n++] = i;
}
void turn(char s[], int i){
switch (i)
{
case 2:
turnV(s); turnV(s); turnV(s); break;
case 3:
turnH(s); turnH(s); turnH(s); turnV(s); break;
case 4:
turnH(s); turnV(s); break;
case 5:
turnV(s); break;
case 6:
turnV(s); turnV(s); break;
default:
break;
}
}
//从上往下看逆时针,1在顶
void turnH(char s[]){
char t = s[2];
s[2] = s[3];
s[3] = s[5];
s[5] = s[4];
s[4] = t;
}
//从右往左看逆时针,1在顶,2在前
void turnV(char s[]) {
char t = s[1];
s[1] = s[5];
s[5] = s[6];
s[6] = s[2];
s[2] = t;
}
原题
UVa253