|
|
back to boardHelp me, please!!! I don't know what is wrong with this!!! Posted by Flyer 3 Apr 2002 14:32 #include <iostream.h> #include <stdio.h> float A,B; int K; float S (long a, long b) { return a*A+b*B-a*a-b*b; } float Maxsb (long a, long& b) { b=B/2; if (b<0) b=0; if (b+a>K) b=K-a; return S (a,b); } int main () { cin >> A >> B >> K; float max=0,maxsb; int ma=0,mb=0; long a,b; for (a=0;a<=K;a++) { maxsb=Maxsb (a,b); if (maxsb>max) { max=maxsb; ma=a; mb=b; } } printf ("%.2f\n",max); printf ("%d %d\n",ma,mb); return 0; } Re: Help me, please!!! I don't know what is wrong with this!!! I think that operator b=B/2 is incorrect. b must be closest integer for B/2. For example, if A=2, B=1.8 and K=100 then rigth answer is ma=1 and mb=1. could you tell me why b=B/2 ? Posted by aaakkk 19 Nov 2002 09:26 > I think that operator b=B/2 is incorrect. b must be closest integer for B/2. > For example, if A=2, B=1.8 and K=100 then rigth answer is ma=1 and mb=1. Re: could you tell me why b=B/2 ? the maximum of a 2 degree function is at point -b/2a, in our case b/2... caterinca brute force works fine... :D |
|
|