SDUT 1194 余弦

Problem Description
输入n的值,计算cos(x)。

Input
输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。
Output
输出cos(x)的值,保留4位小数。
Example Input
0.0 100
Example Output
1.0000
 
 
正确答案:
#include <iostream>
#include <cmath>
//#include <string>
//#include <stdio.h>
#include <iomanip>

//#define  PI 3.1415927
using namespace std;

double arr(int m)
{
	if(m == 0)
		return 1.0;
	double f = 1.0;
	for(; m > 0; m--)
	{
		f *= m;
	}
	return f;
}

int main()
{
	double x, n, result = 0;
	while (cin >> x >> n)
	{
			for (int i = 0; i <= n; i++)
			{
				result += pow(-1, double(i)) * pow(x, double(2 * i)) / arr(2 * i);
			}
			cout << fixed << setprecision(4) << result << endl;
			result = 0;//之前一直错在这里
	}
	return 0;
}

之前的错误答案改错花费很长时间,错在忘记输出之后将result变为0,所以报错:Wrong answer。认真认真再认真!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值