*max_element *min_element
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int n[]= {1,4,22,3,8,5};
int len=sizeof(n)/sizeof(int);
cout<<*max_element(n,n+len)<<endl;
cout<<*min_element(n,n+len)<<endl;
return 0;
}
C++ STL 求向量中的最大值和最小值min_element(v.begin(),v.end()) max_element(v.begin(),v.end()) sizeof(n)/sizeof(int)
min_element 算法返回最小的元素的位置中序列 [first, last)。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n[]= {1,4,2,3,5,0};
vector<int>v(n,n+sizeof(n)/sizeof(int));//sizeof(n)/sizeof(int)是求数组n的长度
cout<<*min_element(v.begin(),v.end())<<endl;//最小元素
cout<<*max_element(v.begin(),v.end())<<endl;//最大元素
return 0;
}
练手题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=218
题目就是要用贪心思想来求。。。。。。,最先完成的机器总是找需要花费时间最长的作业来进行,最终求得答案
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; bool comp(int x,int y) { return x>y; } int main() { int ncases,n,m,i,a[10010],ans[101]; scanf("%d",&ncases); while(ncases--) { memset(a,0,sizeof(a)); memset(ans,0,sizeof(ans)); scanf("%d %d",&n,&m); for(i=0; i<=n-1; i++) { scanf("%d",&a[i]); } sort(a,a+n,comp); for(i=0; i<=n-1; i++) { *min_element(ans,ans+m)=*min_element(ans,ans+m)+a[i]; } printf("%dn",*max_element(ans,ans+m)); } return 0; }