Counterfeit Dollar

GitHub跳转原题关系图返回列表

题意与原解析均从本地 OpenJudge 缓存迁移。

OJ: noi_openjudge

题目 ID: ch0201-15

难度:未知

标签:枚举模拟python

日期: 2026-07-30 23:01

题意

完整题面见同目录的 problem.md

思路

代码

Python代码

python
case_count = int(input())

for _ in range(case_count):
    weighings = [input().split() for _ in range(3)]
    for coin in "ABCDEFGHIJKL":
        for label, weight in (("light", -1), ("heavy", 1)):
            valid = True
            for left, right, result in weighings:
                difference = weight * (left.count(coin) - right.count(coin))
                observed = "even" if difference == 0 else "up" if difference > 0 else "down"
                if observed != result:
                    valid = False
                    break
            if valid:
                print(f"{coin} is the counterfeit coin and it is {label}.")
                break
        else:
            continue
        break

C++代码

cpp
#include <bits/stdc++.h>
using namespace std;

int n;

int weight[500]; //每个coin的重量

/* 结构体 存每一次比较 */
struct compare_ {
    char left[100];
    char right[100];
    int res; // 0 even 1 up 2 down
};

compare_ cmp[5]; // 数组 存每一次比较
bool compare_function(int idx){ // 第 idx 次比较是否正确
    int w_l =0,w_r=0;
    int len,i,res;
    len = strlen(cmp[idx].left+1);
    for(i=1;i<=len;i++){
        w_l += weight[ cmp[idx].left[i] ] ;
    }
    len = strlen(cmp[idx].right+1);
    for(i=1;i<=len;i++){
        w_r += weight[ cmp[idx].right[i] ] ;
    }
    if( w_l == w_r)
        res = 0;
    else if( w_l < w_r)
        res = 2;
    else
        res = 1;
    if( res == cmp[idx].res)
        return 1;
    return 0;
}

/* 数据读取 */
void init(){
    int i;
    char tmp[100];
    for (i=1;i<=3;i++){
        scanf("%s",cmp[i].left+1);
        scanf("%s",cmp[i].right+1);
        scanf("%s",tmp);
        if( tmp[0] == 'e')
            cmp[i].res =0;
        else if ( tmp[0] == 'u')
            cmp[i].res =1;
        else
            cmp[i].res =2;
    }
}
int main(){
    scanf("%d",&n);
    while (n--) {
        init();
        char i;
        // 枚举 weight
        for(i='A';i<='L';i++){
            memset(weight,0,sizeof(weight));
            weight[i] = -1; // 轻
            if( compare_function(1)&&
                    compare_function(2)&&
                    compare_function(3) ) {
                printf("%c is the counterfeit coin and it is light.\n",i);
                break;
            }

            weight[i] = 1; //重
            if( compare_function(1)&&
                    compare_function(2)&&
                    compare_function(3) ) {
                printf("%c is the counterfeit coin and it is heavy.\n",i);
                break;
            }

        }
    }
    return 0;
}

复杂度

总结