当前位置:网站首页>暴力解决《猜名次》问题
暴力解决《猜名次》问题
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。
边栏推荐
- Cannot retrieve repository metadata 处理记录
- JS text particle dynamic background
- Sd6.22 summary of intensive training
- SD6.23集训总结
- 压测并发数的确定
- Basis of data analysis -- prediction model
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
- [Nanjing University] information sharing of the first and second postgraduate entrance examinations
- Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
- Leetcode 984. String without AAA or BBB (thought of netizens)
猜你喜欢

These advantages of the Institute are really fragrant! The landing rate is still very high!

Svg circle drawing path animation

Request header field XXXX is not allowed by access control allow headers in preflight response

《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26

剑指 Offer 34. 二叉树中和为某一值的路径-dfs法

山东大学项目实训(七)添加导航栏选择城市

js文本粒子动态背景

markdown知识轻轻来袭

Machine learning 8-artificial neural network

Panda Parkour JS games code
随机推荐
Sd6.25 summary of intensive training
第三方工具與框架集成
山东大学项目实训(八)设计轮播图进入页面
The table ‘table_ Name 'is full exception troubleshooting and solution
《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26
什么是多范式编程语言,其中的“多范式”是什么意思?
Machine learning 7-Support vector machine
SD6.22集训总结
优雅书写Controller(参数验证+统一异常处理)
75.二叉搜索树额最近公共祖先
BeanUtils属性复制的用法
garbage collector
Svg circle drawing path animation
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
Elegant writing controller (parameter verification + unified exception handling)
Error building sqlsession problem
Programmer Resource Recommendation Guide
layer. prompt
tp5 where查询数据库中的某个字段是否包含某个值,不用like的方法,模糊查询
[daily training] 535 Encryption and decryption of tinyurl