Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题

本文详细介绍了如何通过枚举法解决Counterfeit Dollar问题,并提供了完整的代码实现。利用准确的平衡秤,Sally Jones能够在三次称重内找出唯一的假银币,并判断其重量是轻还是重。

1.链接地址:

http://poj.org/problem?id=1013

http://bailian.openjudge.cn/practice/2692

http://bailian.openjudge.cn/practice/1013

2.题目:

Counterfeit Dollar
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 37454 Accepted: 11980

Description

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.

Input

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.

Output

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.

Sample Input

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 

Sample Output

K is the counterfeit coin and it is light. 

Source

3.思路:

枚举法,枚举全部情况即可

4.代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string str_left[3];
10     string str_right[3];
11     string str_res[3];
12 
13     int n;
14     cin>>n;
15 
16     int i,j,k;
17     
18     while(n--)
19     {
20         for(i = 0; i < 3; ++i) cin>>str_left[i]>>str_right[i]>>str_res[i];
21         for(i = 0; i < 12; ++i)
22         {
23             for(j = 0; j < 2; ++j)
24             {
25                 for(k = 0; k < 3; ++k)
26                 {
27                     char ch = 'A' + i;
28                     string::size_type idx_left = str_left[k].find(ch);
29                     string::size_type idx_right = str_right[k].find(ch);
30 
31                     if(idx_left != string::npos)
32                     {
33                         if(j == 0) //heavy
34                         {
35                             if(str_res[k] != "up") break;
36                         }
37                         else
38                         {
39                             if(str_res[k] != "down") break;
40                         }
41                     }
42                     else if(idx_right != string::npos)
43                     {
44                         if(j == 0)
45                         {
46                             if(str_res[k] != "down") break;
47                         }
48                         else
49                         {
50                             if(str_res[k] != "up") break;
51                         }
52                     }
53                     else
54                     {
55                         if(str_res[k] != "even") break;
56                     }
57 
58                 }
59                 if(k >= 3) break; 
60             }
61             if(j < 2) break;
62         }
63         if(j == 0) cout<<(char)('A' + i)<<" is the counterfeit coin and it is heavy."<<endl;
64         else cout<<(char)('A' + i)<<" is the counterfeit coin and it is light."<<endl;
65     }
66     return 0;
67 }

 

转载于:https://www.cnblogs.com/mobileliker/p/3546623.html

【多种改进粒子群算法进行比较】基于启发式算法的深度神经网络卸载策略研究【边缘计算】(Matlab代码实现)内容概要:本文围绕“基于多种改进粒子群算法比较的深度神经网络卸载策略研究”展开,聚焦于边缘计算环境下的计算任务卸载优化问题。通过引入多种改进的粒子群优化(PSO)算法,并与其他启发式算法进行对比,旨在提升深度神经网络模型在资源受限边缘设备上的推理效率与系统性能。文中详细阐述了算法设计、模型构建、优化目标(如延迟、能耗、计算负载均衡)以及在Matlab平台上的代码实现过程,提供了完整的仿真验证与结果分析,展示了不同算法在卸载决策中的表现差异。; 适合人群:具备一定编程基础和优化算法知识,从事边缘计算、人工智能部署、智能优化等相关领域的科研人员及研究生;熟悉Matlab仿真工具的开发者。; 使用场景及目标:①研究边缘计算环境中深度学习模型的任务卸载机制;②对比分析多种改进粒子群算法在复杂优化问题中的性能优劣;③为实际系统中低延迟、高能效的AI推理部署提供算法选型与实现参考; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点关注算法实现细节与参数设置,通过复现仿真结果深入理解不同启发式算法在卸载策略中的适用性与局限性,同时可拓展至其他智能优化算法的对比研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值