OJ_1150 Counterfeit Dollar

通过三次精确称重找出十二枚银币中的假币及其轻重状态。利用C++编程实现称重逻辑并确定假币。
#include <iostream>
using namespace std;
const int N=3;
int a[128];
void init()
{
     for(int i=0;i<128;i++)
             a[i]=0;
 }
 int abs(int n)
 {
     if(n>=0)return n;
     return 0-n;
 }
void func()
{
     int n;
     while(cin>>n)
     {
                  for(int i=0;i<n;i++)
                  {
                          init();
                          for(int j=0;j<N;j++)
                          {
                                  string s1,s2,judge;
                                  cin>>s1>>s2>>judge;
                                  if(judge=="even")
                                  {
                                          for(int i=0;i<s1.size();i++)
                                          {
                                                  a[s1[i]]=1;
                                          }         
                                          for(int i=0;i<s2.size();i++)
                                          {
                                                  a[s2[i]]=1;
                                          }         
                                          
                                  }
                                  else
                                  if(judge=="up")
                                  {
                                        for(int i=0;i<s1.size();i++)
                                          {
                                                if(a[s1[i]]!=1)
                                                  a[s1[i]]+=2;
                                          }         
                                          for(int i=0;i<s2.size();i++)
                                          {
                                                 if(a[s2[i]]!=1)
                                                  a[s2[i]]-=2;
                                          }           
                                  }
                                  if(judge=="down")
                                  {
                                        for(int i=0;i<s1.size();i++)
                                          {
                                                if(a[s1[i]]!=1)
                                                  a[s1[i]]-=2;
                                          }         
                                          for(int i=0;i<s2.size();i++)
                                          {
                                                 if(a[s2[i]]!=1)
                                                  a[s2[i]]+=2;
                                          }           
                                  }
                          }
                          int k=0;
                          for(int i=0;i<12;i++)
                          {
                                  if(abs(a['A'+i])>abs(a['A'+k]))
                                                             k=i;
                          }
                          if(a['A'+k]<0)
                                    cout<<(char)('A'+k)<<" is the counterfeit coin and it is light."<<endl;
                           else
                                    cout<<(char)('A'+k)<<" is the counterfeit coin and it is heavy."<<endl;         
                  }
     }
     
}
int main(int argc, char *argv[])
{
    
	//printf("Hello, world\n");
	func();
	return 0;
}

even,所有字母加1重量

up,重+2,轻-2

down重+2,轻-2

最后绝对值大者即为重量不标准的那一个


题目描述:

    Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins. 
    Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively. 
    By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.

输入:

    For each case, the first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.

输出:

    For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.

样例输入:
1
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 
样例输出:
K is the counterfeit coin and it is light.

### JarvisOJ FM 功能概述 JarvisOJ 是一个在线编程练习平台,旨在帮助用户提升其逆向工程技能。其中的 FM(File Management)模块主要用于文件管理操作,涉及二进制文件分析、调试以及反汇编等内容[^1]。 FM 的核心功能包括但不限于以下几个方面: - **文件上传与解析** 用户可以通过该模块上传目标文件并对其进行初步解析,提取基本信息以便后续处理。 - **动态调试支持** 提供了一个集成环境用于运行和调试程序,允许设置断点、查看寄存器状态及内存数据等操作。 - **静态反汇编展示** 对于上传的目标可执行文件,能够生成对应的汇编代码视图,便于理解程序逻辑结构。 以下是实现简单文件读取的一个 Python 示例代码片段: ```python def read_binary_file(file_path): try: with open(file_path, 'rb') as file: content = file.read() return content except Exception as e: print(f"Error reading the binary file: {e}") ``` 此代码展示了如何打开并读取一个二进制文件的内容到内存中,这是许多基于文件的操作的第一步,在 JarvisOJ FM 中可能被用来加载待分析样本。 ### 常见问题解答 当使用 JarvisOJ FM 进行学习时可能会遇到一些典型疑问如下: #### 文件无法成功上传? 这可能是由于服务器端配置限制或者客户端网络连接不稳定引起的问题。建议检查所选文件大小是否超出规定上限,并确认当前互联网状况良好再尝试重新提交作业。 #### 调试过程中崩溃怎么办? 如果在利用内置工具对项目进行单步跟踪期间发生异常终止现象,则需仔细核查输入参数合法性;另外也要留意是否存在未初始化变量引用等情况造成非法访问错误。 #### 如何更高效地阅读大量汇编码? 除了依赖自动化插件辅助外,培养扎实的基础理论知识同样重要。平时应多加实践各类算法转换过程中的机器指令表达形式,逐渐形成快速定位关键路径的能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值