ZOJ Monthly, March 2018 A Easy Number Game
生活随笔
收集整理的這篇文章主要介紹了
ZOJ Monthly, March 2018 A Easy Number Game
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5711
簽到題,題意是從n個數中找出m對數相乘求和。直接sort排序一下,然后每次取第i大的數和第2*m-1-i大的數相乘求和得到答案。
#include<iostream> #include<cstring> #include<algorithm> using namespace std; int T; int n,m; int s[100100]; int main() {cin>>T;while(T--){cin>>n>>m;for(int i=0;i<n;i++)cin>>s[i];sort(s,s+n);long long int res=0;for(int i=0;i<m;i++){res+=s[i]*s[2*m-i-1];}cout<<res<<endl;}return 0; }
簽到題,題意是從n個數中找出m對數相乘求和。直接sort排序一下,然后每次取第i大的數和第2*m-1-i大的數相乘求和得到答案。
#include<iostream> #include<cstring> #include<algorithm> using namespace std; int T; int n,m; int s[100100]; int main() {cin>>T;while(T--){cin>>n>>m;for(int i=0;i<n;i++)cin>>s[i];sort(s,s+n);long long int res=0;for(int i=0;i<m;i++){res+=s[i]*s[2*m-i-1];}cout<<res<<endl;}return 0; }
總結
以上是生活随笔為你收集整理的ZOJ Monthly, March 2018 A Easy Number Game的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库设计基础:数据字典相关知识笔记
- 下一篇: 如何禁用计算机的服务,如何彻底禁用电脑中