|
|
back to boardC++ , sort - undefined wtf? Posted by Ivailo 12 Nov 2009 13:53 #include <iostream> using namespace std; int main (){ int n,a[20],m,m1,m2; cin>>n; for (int i=0;i<n;i++){ cin>>a[i]; } sort (a , a+n ); ....... It says that sort (a , a+n )is undefined but when I compile it runs without a problem and it sorts the array a[n].What should I do ?! Re: C++ , sort - undefined wtf? Add - #include <algorithm> Re: C++ , sort - undefined wtf? Posted by Ivailo 12 Nov 2009 18:05 Ok it worked 10x but when I submit my program it says WA on test#1 Here is how I done it: #include <iostream> #include <algorithm> using namespace std; int main (){ int n,a[20],m,m1,m2; cin>>n; for (int i=0;i<n;i++){ cin>>a[i]; } sort (a , a+n ); if (n%2==0){ for (int i=0 , j=n-1;i<(n/2)-1 , j>n/2 ;i=i+2 , j=j-2){ m1=a[i]+a[j]; } for(int i=1 , j=n-2;i<(n/2)-1 , j> n/2; i=i+2 , j=j-2){ m2=a[i]+a[j]; } if(m1>=m2){ m1=m1+a[(n/2)-1]; m2=m2+a[n/2]; } if(m1<=m2){ m1=m1+a[n/2]; m2=m2+a[(n/2)-1]; } } if (n%2!=0){ for (int i =0 , j=n-1 ; i<n/2 , j>n/2 ; i=i+2 , j=j-2){ m1=a[i]+a[j]; } for (int i =1 , j=n-2 ; i<n/2 , j>n/2 ; i=i+2 , j=j-2){ m2=a[i]+a[j]; } if(m1>=m2){ m2=m2+a[n/2]; } else { m1=m1+a[n/2]; } } m=m1-m2; cout<<abs(m); system("pause"); return 0; }
Re: C++ , sort - undefined wtf? It is not a sorting problem! You really don't need to sort! You could check all possible results,and find the best! Or you could use DP! |
|
|