整数 (Integer)
题目描述
在比赛中,两人不分胜负,两人玩得非常开心,当然也非常累,于是他们睡了一觉。当他们醒来时,他们发现有一串数字包围了他们:2.2360679774997896964091736687313…乔普一眼认出了这是sqrt(5)的小数部分。大家都知道这是无限不循环小数,所以普雷斯提议计算(3+sqrt(5)) n次幂的整数部分。这可难倒的乔普啊,聪明的你来帮帮乔普吧!
输入
一个整数n,表示n次幂。
输出
结果的整数部分最后3位。如果结果不超过2位,请补足前导0。
样例输入
【样例1】
2
【样例2】
5
样例输出
【样例1】
027
【样例2】
935
提示
【样例说明】
样例1: 3+sqrt(5)的2次方约等于27.41640786,因此整数部分最后3位补足前导0之后是027;
样例2:(3+sqrt(5))^5约等于3935.739820, ,因此整数部分最后3位是935。
【数据范围约定】
30% 2≤ n ≤ 10;
50% 2≤ n ≤ 50;
70% 2≤ n ≤ 10000;
100% 2≤ n ≤ 2000000000。
想要知道代码,得知道sqrt()和pow() 的含义
/pow()原型:在TC2.0中原型为extern float pow(float x, float y); ,
而在VC6.0中原型为double pow( double x, double y );
头文件:math.h/cmath(C++中)
功能:计算x的y次幂。
返回值:x不能为负数且y为小数,或者x为0且y小于等于0,返回幂指数的结果。
返回类型:double型,int,float会给与警告!/
试用
//pow.c
#include<syslib.h>
#include<math.h>
main()
{
clrscr();//clearscreen
textmode(0x00);//6linesperLCDscreen
printf("4^5=%f",pow(4.,5.));
getchar();
return 0;
}
sqrt功 能: 一个非负实数的平方根
函数原型: 在VC6.0中的math.h头文件的函数原型为double sqrt(double);
说明:sqrt系Square Root Calculations(平方根计算),通过这种运算可以考验CPU的浮点能力。
试用
#include <iostream>
//这里的cmath等价于C的math.h
#include <cmath>
using namespace std;
int main()
{
double x, result;
cin>>x;
result=sqrt(x);
cout<<x<<"的平方根是"<<result<<endl;
return 0;
}
//cmath等价于math.h,其就是using math.h的函数
//VC 2008后为重载函数,原型为 float sqrt (float),double sqrt (double),long double sqrt(long double)
//1 注意没有sqrt (int),但是返回值可以为int
源代码
#include<bits/stdc++.h>
using namespace std;
int main() {
double temp,n;
int ans;
while(cin>>n) {
temp=3+sqrt(5);
ans=pow(temp,n);
if(ans<100) cout<<"0"<<ans<<endl;
else if(ans>999) cout<<ans%1000<<endl;
else cout<<ans<<endl;
}
return 0;
}