【问题标题】:You are given an array consisting of n integers. Print the last two digits of the product of its array values [duplicate]给你一个由 n 个整数组成的数组。打印其数组值乘积的最后两位
【发布时间】:2020-05-24 17:27:34
【问题描述】:

输入

输入的第一行包含一个整数 n,它是给定数组中元素的数量。 输入的第二行包含 n 个空格分隔的整数,它们是给定数组的元素。

输出

打印数组值乘积的最后两位。 请注意,您始终需要打印两位数。

约束

1 <= n <= 100
1 <= arr[i] <= 100. arr[i] is the i​th​ element of the given array.

示例 #1

Input
2
25 10
Output
50
Explanation: 25 * 10 = 250

【问题讨论】:

  • 嗨@mihrex,你能澄清一下这里的问题是什么吗?这看起来像是练习的复制粘贴。请写出与此问题相关的具体技术问题,以及您尝试过的方法以及遇到的错误或意外行为。

标签: python arrays list


【解决方案1】:

你可以试试这个(它是 Python3 的版本):

n = int(input())
arr = list(map(int, input().rstrip().split()))

result = 1
for num in arr:
    result = (result * num) % 100

print("{:02d}".format(result))

稍作修改以获得更好的算法:

n = int(input())
arr = list(map(int, input().rstrip().split()))

result = 1
for num in arr:
    result = (result * num) % 100
    if(result==0): break   # 0 multiply by any number still be 0

print("{:02d}".format(result))

【讨论】:

    【解决方案2】:

    Java。

    private static String display(List<Integer> ary) {
            int tempresult = 1;
    
            if (ary.size() == 0)
                return "0";
            else {
                for (int j = 0; j < ary.size(); j++) {
                    tempresult *= ary.get(j);
                }
    
                int result = tempresult % 100;
    
                if (result < 10) {
                    return "0" + result;
                }
    
                else
                    return String.valueOf(result);
            }
    
        }`
    

    输出应该是2个字符,所以如果结果是1个字符需要转换成2个然后打印结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多