题意:给一张天平的图,通过每个字符的ASCII码的二进制表示算出它的重量,判断已给图中天平的状态是否正确。
分析:因为图中天平的状态只有三种,而且天平中的字符都是大写字母,并且都在同一行,所以计算重量时很简单,给26个字母的重量打表。
在计算天平中字符重量的同时,记录天平左右两个称的高度。一开始,我是检测到字母时记录高度,后来发现这样做就忽略了某个称空着的情况,于是改成当检测到第1列或第11列是‘/’时记录称的高度,因为描绘天平的形状的字符是有固定规律的。
最后分类讨论,给出输出。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
char a[10], b[10], tt[20];
int w[26] = {9, 9, 10, 9, 10, 10, 11, 9, 10, 10, 11, 10, 11, 11, 12, 9, 10, 10, 11, 10, 11, 11, 12, 10, 11, 11};//记录(26个字母的重量 / 250)
int main()
{
int t, cnt;
char ch;
int ha, hb, cnta, cntb, suma, sumb;
scanf("%d", &t);
cnt = 1;
a[7] = b[7] = '\0';
while(t--)
{
cnta = cntb = suma = sumb = ha = hb = 0;
for(int i = 1; i <= 7; i++)
{
getchar();
for(int j = 1; j <= 18; j++)
{
scanf("%c", &ch);
if (j == 1 && ch == '/') ha = i;
if (j == 11 && ch == '/' ) hb = i;
if(ch >= 65 && ch <= 90)
{
//printf("ch=%c\n", ch);
if(j <= 7 && j >= 2)
{
a[++cnta] = ch;
//ha = i;
suma += w[ch - 'A'];
}
if(j >= 12 && j <= 17)
{
b[++cntb] = ch;
//hb = i;
sumb += w[ch - 'A'];
}
}
}
}
scanf("%s", tt);
//printf("ha:%d,hb%d\n", ha, hb);
if((ha > hb && suma > sumb) || (ha < hb && suma < sumb) || (ha == hb && suma == sumb))
printf("Case %d:\nThe figure is correct.\n", cnt++);
else
{
while(cnta <6)
{
a[++cnta] = '.';
}
while(cntb <6)
{
b[++cntb] = '.';
}
if(suma == sumb)
{
printf("Case %d:\n", cnt++);
printf("........||........\n");
printf(".../\\...||.../\\...\n");
printf("../..\\..||../..\\..\n");
printf("./....\\.||./....\\.\n");
printf("/%s\\||/%s\\\n",a + 1,b + 1);
printf("\\______/||\\______/\n");
printf("........||........\n");
}
else if(suma < sumb)
{
printf("Case %d:\n", cnt++);
printf(".../\\...||........\n");
printf("../..\\..||........\n");
printf("./....\\.||.../\\...\n");
printf("/%s\\||../..\\..\n",a + 1);
printf("\\______/||./....\\.\n");
printf("........||/%s\\\n",b + 1);
printf("........||\\______/\n");
}
else if(suma > sumb)
{
printf("Case %d:\n", cnt++);
printf("........||.../\\...\n");
printf("........||../..\\..\n");
printf(".../\\...||./....\\.\n");
printf("../..\\..||/%s\\\n", b + 1);
printf("./....\\.||\\______/\n");
printf("/%s\\||........\n", a + 1);
printf("\\______/||........\n");
}
}
}
}