|
|
вернуться в форумproblem with the task\ С++\ Задача 1005 Послано Giorgi 13 янв 2011 02:16 program correctly solves all problems, but for some reason the site shows that the program is wrong ... can you explain why? #include<iostream> using namespace std; int main() { double ves_k[20]; int i,num; double t,perv,vtor,res; cin>>num; if(num<1||num>20) goto vozvrat; for(i=0; i<num; i++){ cin>>ves_k[i]; if(ves_k[i]<1||ves_k[i]>10000) goto vozvrat; } if(num!=1){ for(int a=1; a<i; a++) for(int b=i-1; b>=a; b--){ if(ves_k[b-1]>ves_k[b]) { t=ves_k[b-1]; ves_k[b-1]=ves_k[b]; ves_k[b]=t; } } perv=ves_k[num-1]; vtor=ves_k[num-2]; for(i=num-3; i>=0; i--){ if(perv>=vtor) vtor+=ves_k[i]; else perv+=ves_k[i]; } res=perv-vtor; if(res<0) res=-res; } else res=ves_k[0];
cout<<res; vozvrat: return 0; } Edited by author 13.01.2011 02:22 Re: problem with the task\ С++\ Задача 1005 print out your answer with end line. cout << res << endl; Re: problem with the task\ С++\ Задача 1005 Greedy algorithm is wrong! You should check all 2^n situations. Re: problem with the task\ С++\ Задача 1005 Послано Giorgi 13 янв 2011 17:36 Greedy algorithm is wrong! You should check all 2^n situations. why the wrong ... it calculates all the results correctly, you can check yourself Re: problem with the task\ С++\ Задача 1005 You can find some tests on the forum and check yourself that greedy algo is wrong. Re: problem with the task\ С++\ Задача 1005 Послано Giorgi 13 янв 2011 23:25 I know that the greedy algorithm is not always appropriate, but in this case, it should work |
|
|