0

我有以这种形式编写的输入字符串:

S = 2222+576666

我需要帮助 for-loop 和“+”符号来提取“+”之前和之后的数字

for position in range(0,len(S)):
      # Missing part of code I need help with to get desired solution
      number1 = 2222
      number2 = 576666

我不允许使用任何导入或任何花哨的函数,如 split() 或 raw_input()。

4

2 回答 2

1

这个怎么样:

s = "2222+576666"
l = [""] * 2
i = 0
for c in s:
    if c == '+':
        i = 1
        continue
    l[i] += c
num1 = int(l[0])
num2 = int(l[1])
于 2012-09-05T23:15:44.353 回答
0

这应该工作

counter = 0
for position in range(0,len(S)):
    if S[position] <> '+':
       astr[counter] = S[position]
      counter = counter+1
    else:
      number1 = int(astr)
       astr = ''
       counter = 0
number2 = int(astr2)
于 2012-09-05T23:22:28.983 回答