找到数字的整数部分的各位数字之和,以及小数部分的各位数字的乘积。

huangapple go评论66阅读模式
英文:

find the sum of the digits of the integer part of the number and the multiplication of the digits of the fractional part of the number

问题

整数部分的数字之和以及小数部分的数字之积。我只能输出整数部分的答案。

英文:

find the sum of the digits of the integer part of the number and the multiplication of the digits of the fractional part of the number.

I can only output the answer to the integer part of the number.

答案1

得分: 1

只返回翻译好的部分,不包括代码:

  1. 将数字分成整数部分和小数部分:
    可以使用内置的 divmod 函数。例如,divmod(7.86, 1) 返回 (7, 0.86)

  2. 找到整数部分的数字之和。可以用多种方法来实现,性能有所不同。可以将整数部分转换为字符串,然后迭代字符串的字符并将值添加到一个变量中。

  3. 类似地,可以将小数部分转换为小数点后的字符串,然后迭代字符串的字符并将值与一个变量相乘。

  4. 返回上述步骤的两个结果。

英文:

You didn't show us what you tried, so i'll give you only some steps and leave the implementation for you to do by yourself.

  1. Split the number into integer and fractional parts:
    You can use the built-in divmod function. For example, divmod(7.86, 1) returns (7, 0.86).

  2. Find the sum of the digits of the integer part. This can be done in multiple ways with varying performance.You can convert the integer part to a string, then iterate over the characters of the string and add the values to a variable.

  3. Similarly, you can convert the fractional part to a string after the decimal point, then iterate over the characters of the string and multiply the values with a variable.

  4. Return the two results of the steps above.

Edit: The code you posted in the comment can be fixed as follows. You want to escape the iteration when hitting the . symbol, and make the check first because the user might enter .456.

s = input() 
letsGo = summa = 0
proizved = 1
for i in s:
    if i == ".":
        letsGo = 1
        continue
    if letsGo == 1:
        proizved *= int(i)
    else:
        summa += int(i)

huangapple
  • 本文由 发表于 2023年2月8日 20:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定