程序在whileloop期间忽略ifs语句

2024-04-27 22:21:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python程序,它使用给定的公式更新坐标:(x,y) = (ax-by, ay+bx)。这应该针对每个步骤(n)进行,该步骤由c+1个微步骤组成。因此,基本上,对于4个步骤中的每一步,我都会更新位置3次

import math
import os
import random
import re
import sys

#
# Complete the 'findPosition' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER s
#  2. INTEGER x0
#  3. INTEGER y0
#  4. INTEGER a
#  5. INTEGER b
#  6. INTEGER c
#  7. INTEGER l
#  8. INTEGER n
#

def findPosition(s, x0, y0, a, b, c, l, n):
    # Write your code here
    xf = yf = 0
    t = 0
    while (n > 0):
        while (t <= c):
            xf = (a * x0) - (b * y0)
            yf = (a * y0) + (b * x0)
            if (xf >= s):
                xf = xf - s
            if (yf >= s):
                yf = yf - s
            if (xf < 0):
                xf = xf + s
            if (yf < 0):
                yf = yf + s
            x0 = xf
            y0 = yf
            #print (xf, yf)
            t = t + 1
        t = 0
        n = n - 1
    return (xf, yf)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()
    a = int(first_multiple_input[0])
    b = int(first_multiple_input[1])
    c = int(first_multiple_input[2])
    l = int(first_multiple_input[3])
 
    second_multiple_input = input().rstrip().split()
    s = int(second_multiple_input[0])
    x0 = int(second_multiple_input[1])
    y0 = int(second_multiple_input[2])
    n = int(second_multiple_input[3])
 
    result = findPosition(s, x0, y0, a, b, c, l, n)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')
    fptr.close()

问题是,我的代码在程序中的某个特定时间忽略了我的if语句(在步骤3的微步1期间)。 我应该得到的输出是:

(4, 7)  (1, 18) (7 ,14)
(0, 12) (11, 1) (21, 13)
(6, 1)  (11, 8) (14, 4)
(1, 22) (3, 22) (7, 1)

我实际上得到的是:

(4, 7)  (1, 18)  (7, 14)
(0, 12)  (11, 1)  (21, 13)
(6, 24)  (11, 31)  (14, 50)
(1, 91)  (-66, 160)  (-269, 231)

这是用于输入:a=2b=1c=2l=1s=23x0=3y0=2n=4


Tags: importinputif步骤integermultipleintfirst
1条回答
网友
1楼 · 发布于 2024-04-27 22:21:26

它不会忽略您的if语句。你的if语句不能反映你需要什么。在第六步中,xf,yf变成(29,47)。47>;23,减去23得到24。不允许xf或yf大于s的两倍。您需要的是模运算符,如下所示。哦,请注意,在Python中,ifwhilereturn语句不需要括号

def findPosition(s, x0, y0, a, b, c, l, n):
    for i in range(n):
        for t in range(c+1):
            xf = ((a * x0) - (b * y0)) % s
            yf = ((a * y0) + (b * x0)) % s
            x0 = xf
            y0 = yf
            print (i, t, c, s, xf, yf)
    return xf, yf

相关问题 更多 >