数的乘积

数的乘积

考虑用除法解决这个问题。因为如果这些数的乘积超过了 1018,那么用 1018 依次除以这些数肯定存在一个时刻变为 0。所以就可以在不使用 __int128 这类黑科技的情况下方便的判断。注意如果有一个数是 0 应该立刻停下输出 0,不然可能出现 Float Point Exception

#include<bits/stdc++.h>
using namespace std;
#define L(i,l,r) for(int i=l;i<=r;++i)
#define R(i,l,r) for(int i=r;i>=l;--i)
typedef long long ll;
ll a=1e18,b=1;
bool flag;
int main(){
    // freopen("1.in","r",stdin);
    // freopen("1.out","w",stdout);
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    // cout.tie(0);
    int n;
    scanf("%d",&n);
    while(n--){
        ll x;
        scanf("%lld",&x);
        if(!x)return puts("0"),0;
        a/=x;
        if(!a)flag=1;
        b*=x;
    }
    printf("%lld",flag?-1:b);
    return 0;
}

本文作者:wscqwq

本文链接:https://www.cnblogs.com/wscqwq/p/17307961.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wscqwq  阅读(21)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起