1580 - 重载运算符

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

建立一个角类,在这个类中重载减号运算符角度相减,并实现求出角度的正弦值的函数

题目输入

输入第一行为样例数m,接下来有m行每行两个整数表示角度。

题目输出

输出m行,表示两角相减的正弦值,保留小数点后两位。

输入/输出样例

输入格式

1
60 30

输出格式

0.50

C语言解答

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

int main(){
	int n,i;
	double a,b,s;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%lf %lf",&a,&b);
		s=a-b;
		s=s*3.1415926/180;
		s=sin(s);
		printf("%.2lf\n",s);
	}
	return 0;
}

C++解答

#include <iostream>
#include <math.h>
#include <cstdio>
using namespace std;

const double pi = acos(-1.0);

class angle
{
    int X;
public:
    double xsin();
    angle() {};
    angle(int x)
    {
        X=x;
    }
    angle operator- (angle c);
};
angle angle::operator- (angle c)
{
    return angle(X-c.X);
}
double angle::xsin()
{
    double x=X*pi/180;
    return sin(x);
}
int main()
{
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    int p, q, t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d", &p, &q);
        angle a(p),b(q),d;
        d=a-b;
        printf("%.2f\n", d.xsin());
    }
    return 0;
}

Java解答



import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    private static Scanner s = new Scanner (System.in) ;
    static DecimalFormat df = new DecimalFormat("0.00") ;
    public static void main(String[] args) {
		int n = s.nextInt() ;
		for (int i = 0; i < n; i++) {
			int x = s.nextInt() ;
			int y = s.nextInt() ;
			System.out.println(df.format(Math.sin(Math.PI*((double)(x-y)/180))));
		}
    }
}