8 solutions

  • 0
    @ 2021-10-17 16:05:29

    将数字存入数组,题目需要经过a*b+1运算后的最大值和最小值。我们讲数组倒序排列,先将大的值去掉剩下小的值,这样经过运算后就得到最小值。同理最大值就将数组顺序排列运算,注意的是经过运算产生新的值会使数组顺序发生变化,所以我们需要在循环里面再排序. #include #include #include<stdio.h> using namespace std; int a[50005],b[50005]; int cmp(int a,int b){ return a>b; } int main(){ int n,max,min; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(a+1,a+1+n,cmp); for(int i=2;i<=n;i++) a[i]=a[i]*a[i-1]+1; min=a[n]; sort(b+1,b+1+n); for(int i=2;i<=n;i++){ b[i]=b[i]*b[i-1]+1; sort(b+1,b+n+1); } max=b[n]; printf("%d\n",max-min); return 0; }

    Information

    ID
    95
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    146
    Accepted
    52
    Uploaded By