简体   繁体   English

这个算法背后的逻辑是什么?

[英]What is the logic behind this algorithm?

The explanation for this problem can be found @ http://www.cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf这个问题的解释可以在@http ://www.cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
(it's the third problem titled "exactly electrical"). (这是题为“完全电气化”的第三个问题)。

def electrical(a,b,c,d,e) :
  charge = e
  x_dif = abs(c - a)
  y_dif = abs(d - b)
  total_dif = (x_dif + y_dif)

  if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0))) :
    return "Y"
  else :
    return "N"

print(electrical(10,2,10,4,5))

This code can also be found at https://repl.it/@erichasegawa/2017-CCC-Junior-S3 .此代码也可以在https://repl.it/@erichasegawa/2017-CCC-Junior-S3找到。

I'm studying to write the Canadian Computing Competition this week, and I have a question about one of their algorithms;我正在学习编写本周的加拿大计算机比赛,我对他们的一种算法有疑问; why will my function return "Y" when both the charge and distance are even or uneven, but if one is even and the other isn't (or vice versa) it returns false.当电荷和距离都是偶数或不均匀时,为什么我的函数会返回“Y”,但是如果一个是偶数而另一个不是(反之亦然),它会返回 false。 I understand that this works, but I don't know why or how it works.我知道这是有效的,但我不知道它为什么或如何工作。 If someone could explain this that would be great.如果有人可以解释这一点,那就太好了。

Breakdown the condition:分解条件:

if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0)))

We have...我们有...

(total dif == charge) # they are equal, so either both odd or even

or ((total_dif % 2 == 0) and (charge % 2 == 0)) # if their remainder after division by 2 is 0, then they're both even 

or ((total_dif % 2 != 0) and (charge % 2 != 0)) # if their remainder after division by 2 is NOT 0, then they're both odd

Note that the first condition is unnecessary;请注意,第一个条件是不必要的; we already check if both are even or if both are odd later on.我们已经检查了两者是偶数还是奇数。 Having or removing should not change the behaviour of the program.拥有或删除不应该改变程序的行为。

Also note that the set of brackets around "total_dif" are unnecessary and make an already huge condition more difficult to read.另请注意,“total_dif”周围的括号集是不必要的,并且会使已经很大的条件更难以阅读。 In fact, you should split up the expression in different parts, perhaps as variables both_even and both_odd, and then check事实上,你应该把表达式拆分成不同的部分,也许作为变量both_even 和both_odd,然后检查

if (both_even or both_odd)

which is much more readable这更具可读性

Your code states您的代码说明

if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0))) :

The first condition第一个条件

(total_dif == charge)

is determining whether the charge is equal to the distance needed, so both are even or odd.正在确定电荷是否等于所需的距离,因此两者都是偶数或奇数。

The second condition第二个条件

((total_dif % 2 == 0) and (charge %2 == 0))

checks if the remainder of both arguments (charge and distance) when divided by two are even (the % operator);检查两个参数(电荷和距离)除以 2 的余数是否为偶数(% 运算符); it is checking if they are both even numbers.它正在检查它们是否都是偶数。

Your third condition does just the opposite, it checks if they are both odd numbers.你的第三个条件正好相反,它检查它们是否都是奇数。

In essence, your conditions are checking if the difference between the charge and the distance is divisible by two, since you can make two U-turns to negate this even surplus.从本质上讲,您的条件是检查电荷和距离之间的差值是否可以被 2 整除,因为您可以进行两次掉头以抵消这个偶数盈余。

Therefore, your conditions could be simplified to因此,您的条件可以简化为

if ((charge-total_dif) % 2 == 0):

Your question is technically irrelevant to the problem, since if one argument is even and the other is odd, you will either have a surplus of a deficit.你的问题在技术上与问题无关,因为如果一个参数是偶数而另一个是奇数,那么你要么有盈余,要么有赤字。 Always.总是。

The only other problem with your code is that it never checks if the charge is greater or equal to the distance !您的代码唯一的另一个问题是它从不检查电荷是否大于或等于距离

This means you can have an even distance n (like 28321728932) with 0 charge and still return 'Y', or have an odd distance x (like 3121) with 3 charge and still return 'Y'.这意味着你可以有一个偶数距离 n(如 28321728932),电荷为 0,但仍返回“Y”,或者具有奇数距离 x(如 3121),电荷为 3,但仍返回“Y”。

Therefore, you should include the condition因此,您应该包括条件

charge >= total_dif

in your code.在你的代码中。

In summary, your condition should be总之,你的情况应该是

if ((charge >= total_dif) and ((charge-total_dif) % 2 == 0):

Final note: please use shorter variable names.最后注意:请使用较短的变量名称。 In a short program such as yours, you won't have trouble distinguishing between shorter names.在像您这样的短程序中,区分较短的名称不会有困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM