24h購物| | PChome| 登入
2012-12-06 20:05:17| 人氣1,590| 回應1 | 上一篇 | 下一篇

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

推薦 0 收藏 0 轉貼0 訂閱站台


  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;
}


台長: Morris
人氣(1,590) | 回應(1)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][博奕] 10368 - Euclid's Game
此分類上一篇:[UVA][博奕][記憶化搜索] 10891 - Game of Sum

許胖
無恥的 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:11:15
版主回應
<(_ _)>
2012-12-08 11:38:48
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文