| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这题有个思路比较清晰的方法这题有个思路比较清晰得解法
首先, int 一个长度为12 的数组 coinArray[12];
bool 一个数组markArray[12];
coinArray[12] 初值为0, markArray[12]初值为false;
如果天平的是even 则对于两盘的硬币X(X是A~L的任意), coinArray[X - 65] = REAL (假定const int REAL = 999)
如果天平是up, 则对于左盘的每个硬币X , 如果coinArray[X - 65] != REAL, 则coinArray[X - 65] += 1, 右盘的话,只要if coinArray[X - 65] != REAL, coinArray[X - 65] -= 1
如果是down, 则对于左盘的每个硬币X , 如果coinArray[X - 65] != REAL, 则coinArray[X - 65] -= 1, 右盘的话,只要if coinArray[X - 65] != REAL, coinArray[X - 65] += 1
最后, 在排除REAL的项中, 取绝对值最大的那个项,设为i, 如果coinArray[i] < 0 则char(i + 65)是假的,且是轻的,else 则是重的
思想: 因为假硬币肯定于up or down 项目中, 可以得出,它对应绝对值一定最大, 如果其他硬币跟它一样大, 则不能判断, 于是就不符合题意
附程序:
#include <iostream>
#include <string>
using namespace std;
const int EMPTY = 0;
const int REAL = 999;
int coinArray[12];
bool markArray[12];
inline void Process(string& leftArray, string& rightArray, string& opArray)
{
int len = leftArray.length(),
displaceLeft, displaceRight;
switch (opArray[0])
{
case 'u':
{
int i;
for (i=0; i<len; i++)
{
displaceLeft = int(leftArray[i] - 65);
displaceRight = int(rightArray[i] - 65);
if (coinArray[displaceLeft] != REAL) coinArray[displaceLeft] += 1;
if (coinArray[displaceRight] != REAL) coinArray[displaceRight] -= 1;
markArray[displaceLeft] = markArray[displaceRight] = true;
}
break;
}
case 'e':
{
int i;
for (i=0; i<len; i++)
{
displaceLeft = int(leftArray[i] - 65);
displaceRight = int(rightArray[i] - 65);
coinArray[displaceLeft] = coinArray[displaceRight] = REAL;
markArray[displaceLeft] = markArray[displaceRight] = true;
}
break;
}
case 'd':
{
int i;
for (i=0; i<len; i++)
{
displaceLeft = int(leftArray[i] - 65);
displaceRight = int(rightArray[i] - 65);
if (coinArray[displaceLeft] != REAL) coinArray[displaceLeft] -= 1;
if (coinArray[displaceRight] != REAL) coinArray[displaceRight] += 1;
markArray[displaceLeft] = markArray[displaceRight] = true;
}
break;
}
default:;
}
}
inline void dealArray()
{
int max = 0,
displace;
char temp[12];
int i;
for (i=0; i<12; i++)
{
if (markArray[i] && coinArray[i] != REAL) temp[i] = (int)abs(coinArray[i]);
}
for (i=0; i<12; i++)
{
if (markArray[i] && coinArray[i] != REAL)
{
if (max < temp[i])
{
max = temp[i];
displace = i;
}
}
}
putchar(char(displace + 65));
printf(" is the counterfeit coin and it is ");
if (coinArray[displace] < EMPTY) puts("light.");
else puts("heavy.");
}
int main()
{
string leftArray,
rightArray,
opArray;
int times;
scanf("%d", ×);
int i;
for (i=0; i<times; i++)
{
int j;
for (j=0; j<12; j++)
{
coinArray[j] = EMPTY;
markArray[j] = false;
}
for (j=0; j<3; j++)
{
cin >> leftArray >> rightArray >> opArray;
Process(leftArray, rightArray, opArray);
}
dealArray();
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator