#12455: 哪裡會錯


0822_137934 (14306)

學校 : 臺北市私立延平高級中學
編號 : 60259
來源 : [119.14.210.98]
最後登入時間 :
2022-12-27 23:22:35

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
long long int t,n,w,k,x,y;
cin>>t;
while(t--)
{cin>>n>>w;
long long int a[n];
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<w;i++)
{cin>>k;
if(k==1) {cin>>x>>y;long long int max=0;
for(int j=x;j<=y;j++){if(a[j]>max)max=a[j];}
cout<<max<<endl;}
else if(k==2){cin>>x>>y;long long int sum=0;
for(int j=x;j<=y;j++){sum+=a[j];}
cout<<sum/(y-x+1)<<endl;}
else {cin>>x;
cout<<a[x]<<endl;
}
}
}
return 0;
}

#12456: Re:哪裡會錯


rex930827 (21許展睿306)

學校 : 臺北市私立延平高級中學
編號 : 60246
來源 : [101.12.27.204]
最後登入時間 :
2022-11-15 18:28:20

#include
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
long long int t,n,w,k,x,y;
cin>>t;
while(t--)
{cin>>n>>w;
long long int a[n];
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<w;i++)
{cin>>k;
if(k==1) {cin>>x>>y;long long int max=0;
for(int j=x;j<=y;j++){if(a[j]>max)max=a[j];}
cout<<max<<endl;}
else if(k==2){cin>>x>>y;long long int sum=0;
for(int j=x;j<=y;j++){sum+=a[j];}
cout<<sum/(y-x+1)<<endl;}
else {cin>>x;
cout<<a[x]<<endl;
}
}
}
return 0;
}

測資有負數,要把max指定成第1個數。