2012-12-06 20:05:17Morris

[UVA][柯西不等式] 12575 - Sin Cos Problem


  Sin Cos Problem 

Given A and B, you have to determine the maximum value of the function :

F($displaystyle theta$) = A*Sin$displaystyle theta$ + B*Cos$displaystyle theta$

Input 

First line of input will contain the number of test cases, T$ le$2000. Then there follows T lines, each containing two integers A and B separated by a single space. A and B will fit in a signed 32bit integer.

Output 

For each case, print one line containing two single space separated real values rounded to two decimal places. The first one is the lowest non-negative value of $ theta$ ($ theta$ is in Radian) for which the F($ theta$) gives maximum value and the second one is the maximum value.


Note: Pi is considered to be arccos(- 1).

Sample Input 

4
1 1
-1 1
1 -1
-1 -1

Sample Input 

0.79 1.41
5.50 1.41
2.36 1.41
3.93 1.41


Problem Setter: Muhammad Ridowan
Alternate Solution: Zobayer Hasan


注意 0 0 的輸入,請輸出 0.00 0.00


#include <stdio.h>
#include <math.h>
#define eps 1e-2
int main() {
    int t, i;
    scanf("%d", &t);
    double A, B, pi = acos(-1);
    while(t--) {
        scanf("%lf %lf", &A, &B);
        if(A == 0 && B == 0) {
            puts("0.00 0.00");
            continue;
        }
        double tmp1, tmp2;
        double F = sqrt(A*A+B*B);
        tmp1 = atan(A/B);
        for(; tmp1 <= 2*pi; tmp1+=pi/2) {
            if(fabs(A*sin(tmp1)+B*cos(tmp1)-F) < eps && tmp1 >= 0)
                break;
        }
        printf("%.2lf %.2lf\n", tmp1, F);
    }
    return 0;
}


許胖 2012-12-08 11:11:15

無恥的 atan2 .........

#include <stdio.h>
#include <math.h>

#define PI acos(-1.0)
#define EPS 5.14e-7

int t;
double a, b, ans;

int main()
{
for (scanf("%d", &t); t; t--)
{
scanf("%lf%lf", &a, &b);
ans = atan2(a, b);
if (ans < -EPS) ans += 2.0 * PI;
printf("%.2lf %.2lf\n", ans, sqrt(a * a + b * b));
}
}

版主回應
<(_ _)> 2012-12-08 11:38:48