Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  training
Language: C/C++
Code:
#include <iostream>
using namespace std; 
int main() 
{
    int t, n, p,i,max=0,sum=0,pos=0,pos1=0;
    double s[10000];
    cin >> t;
    if(t>=1 && t<101)
    {
    for ( i = 1; i <= t; ++i) 
     {
        cin >> n >> p; 
        if(n>=2 && n<1001)
        {
         if(p>=2 && p<=n)
         {
           for(int j=1;j<=n;j++)
           {
            cin >> s[j];   
           }
           max=s[1];
            for(int k=1;k<n;k++)
            {
              if(k==(n-1))
              {
              pos=k;                  
              }
              if(s[k+1]>max)
              {
              max=s[k+1];
              pos1=k+1;
              }
            }
           cout<< " max: " << max << " pos: " << pos;
           if(p==n)
           {
            sum=0;
            for(int k=1;k<=n;k++)
            {
              sum=sum+(max-s[k]);
            }
            cout<< " sum: " << sum;
           }
           else
           {
            max=s[pos];
            sum=0;
            for(int l=1;l<=n;l++)
            {
              sum=sum+(max-s[l]);
            }
            sum=sum-(max-s[pos1]);
            cout<< " sum: " << sum;
           }
         }
        }
     }
    }
    return 0;
}
     
Comments: