当前位置:网站首页>暴力解决《猜名次》问题
暴力解决《猜名次》问题
2022-06-29 18:32:00 【有梦想的咸鱼、】
5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果:
A选手说:B第二,我第三;
B选手说:我第二,E第四;
C选手说:我第一,D第二;
D选手说:C最后,我第三;
E选手说:我第四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
由题可知,该题只有唯一解,并且A,B,C,D,E的名次分为1,2,3,4,5。
那我们不妨设计五次循环,每次循环的变量设为A,B,C,D,E。
让他们每个都循环5次取出所有可能的情况。
在最后加上题里的判断条件。
这里要注意:每个人的每个条件都必须要判断,所以用逻辑或“||”是不可以的
因为如果运用逻辑或会出现不是唯一解。
那既然每个人的条件都是一真一假,那我们不妨设计这样的判断:
(B == 2)+(A == 3)== 1
这样左右都有判断,接下来是代码的实现
#include <stdio.h>
int main()
{
int A, B, C, D, E;
for (A = 1; A <= 5; A++)
{
for (B = 1; B <= 5; B++)
{
for (C = 1; C <= 5; C++)
{
for (D = 1; D <= 5; D++)
{
for (E = 1; E <= 5; E++)
{
if ((B == 2) + (A == 3) == 1 &&
(B == 2) + (E == 4) == 1 &&
(C == 1) + (D == 2) == 1 &&
(C == 5) + (D == 3) == 1 &&
(E == 4) + (A == 1) == 1 &&
A * B * C * D * E == 120)
{
printf("A = %d,B = %d,C = %d,D = %d,E = %d\n", A, B, C, D, E);
}
}
}
}
}
}
return 0;
}
结果是 A = 3,B = 1,C = 5,D = 2,E = 4。
边栏推荐
- Fastdfs
- 什么是多范式编程语言,其中的“多范式”是什么意思?
- 山东大学项目实训(七)添加导航栏选择城市
- 第三方工具与框架集成
- Tag filtering and SQL filtering of rocketmq
- Shandong University project training (VII) add navigation bar to select city
- Request header field XXXX is not allowed by access control allow headers in preflight response
- Data analysis time series prediction
- Shandong University project training (VI) Click event display line chart
- Selenium WebDriver的高级特性
猜你喜欢

产品-Axure9(英文版),中继器(Repeater)实现表格内容的增删查改(CRUD)

对强缓存和协商缓存的理解

Redis(一)--Redis入门(2)--Redis数据类型

How to use an oak camera as a webcam?

Deep learning --- the weight of the three good students' scores (2)

sql server备份以及还原的命令操作

第02章_MySQL的数据目录

Cannot retrieve repository metadata processing records

2. 在STM32CubeMX建立的Keil5工程中添加自定义的相关文件

数据仓库模型分层ODS、DWD、DWM实战
随机推荐
Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
76.二叉树的最近公共祖先
data-*属性用法
[how the network is connected] Chapter 3 explores hubs, switches and routers
如何在树莓派上使用OAK相机?
Redis(一)--Redis入门(1)--Redis介绍、安装与启动、常用配置
Failed to allocate graph: myriad device is not opened
Chapter 02_ MySQL data directory
Apache inlong million billion level data stream processing
2. 银河麒麟离线模式下如何安装MySQL数据库
Shell基本语法--流程控制
js将秒转换成“2h30min50s”的格式
Adobe Premiere foundation - time remapping (10)
Goldfish rhca memoirs: do447 build advanced job workflow -- create workflow job template and start workflow job
Basis of data analysis -- prediction model
About microservices
garbage collector
Sword finger offer 34 Path DFS method for binary tree neutralization
WBF: new method of NMS post filter frame for detection task?
踩坑记:JSON.parse和JSON.stringify