爆0求救!下面的Python的这个解,洛谷测试全部WA。求大侠指点

P1957 口算练习题

Python应该是被卡了
by 百因必有AC @ 2020-04-05 17:54:06


洛谷IDE,你值得拥有
by 警策看取 @ 2020-04-05 18:00:11


@[百因必有AC](/user/113097) ?
by SSerxhs @ 2020-04-05 18:00:42


@[wuerlang](/user/174157) 看到你的帖子,我现场做了下,顺便表演一波极限压行 ```python def main(op: str, a: int, b: int) -> str: # 返回 last ans = "%d+%d=%d"%(a, b, a+b) if op == 'a' else\ "%d-%d=%d"%(a, b, a-b) if op == 'b' else\ "%d*%d=%d"%(a, b, a*b) if op == 'c' else\ "Fuck! " print(ans); print(len(ans)) return op if __name__ == "__main__": n = int(input()) for i in range(n): tmp = input().split() last = main(last, *map(int, tmp)) if len(tmp) == 2 else\ main(tmp[0], *map(int, tmp[1:])) ``` ~~你的问题我马上调查~~ 本地正常,ide 上面有奇怪的换行,原因正在调查
by Master_Hash @ 2020-04-05 19:03:58


~~都0202年了还有P党?。。。~~
by btng_smith666 @ 2020-04-05 19:04:57


@[wuerlang](/user/174157) 神秘的换行符是 y 的最后一位,但是这个换行符既不是 \r 也不是 \n,特别奇怪 附上修改后的 AC 代码 ```python # P1957 口算练习题 def myprint(op, x, y): if op == 'a': z = int(x) + int(y) print("{:s}+{:s}={:d}".format(x, y, z)) # print(f"{x}+{y}={z}") # print("%s+%s=%d" %(x, y, z)) elif op == 'b': z = int(x) - int(y) print("{:s}-{:s}={:d}".format(x, y, z)) # print("%s-%s=%d" %(x, y, z)) #print(f"{x}-{y}={z}") elif op == 'c': # else: # op=='c' z = int(x) * int(y) print("{:s}*{:s}={:d}".format(x, y, z)) # print("%s*%s=%d" %(x, y, z)) #print(f"{x}*{y}={z}") print(len(x) + len(y) + 2 + len(str(z))) n = int(input()) for i in range(1, n+1): str1 = input() if 'a' <= str1[0] <= 'c': op = str1[0] str1 = str1[2:] pos = str1.index(' ') x = str1[:pos] y = str1[pos+1:] if not '0' <= y[-1] <= '9': y = y[:-1] myprint(op, x, y) ```
by Master_Hash @ 2020-04-05 19:36:58


@[wuerlang](/user/174157) 只加了倒数23行 话说为什么不理我。。。我的压行那么漂亮
by Master_Hash @ 2020-04-05 19:38:05


@[wuerlang](/user/174157) 另外最好不要用字符串处理数字 ~~看起来你好像不会用 split,这个 A+B 里面就讲过吧~~
by Master_Hash @ 2020-04-05 19:39:33


@[Master_Hash](/user/306754) 谢谢!
by wuerlang @ 2020-04-05 22:16:52


@[Master_Hash](/user/306754) split()也测试在用,多种方法练习
by wuerlang @ 2020-04-05 22:17:40


| 下一页