#include<iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
int a,b,k;
while(cin >> a >> b >> k)
{
int temp = a,ans = 1;
while(k){
if(k % 2 == 1)
ans = (ans * temp) % b;
temp = (temp * temp) % b;
k /= 2;
}
cout << ans << endl;
}
return 0;
}- 题目描述:
-
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
- 输入:
-
输入可能包含多个测试样例。
对于每个输入文件,第一行输入一个整数T,表示测试案例的数目,接下来的T行每行输入一个浮点数base和一个整数exponent,两个数中间用一个空格隔开。
- 输出:
-
对应每个测试案例,
输出一个浮点数代表答案,保留两位小数即可。
- 样例输入:
-
5 1.0 10 0.0 -5 1.0 0 1.2 5 2.0 -1
- 样例输出:
-
1.00e+00f INF 1.00e+00f 2.49e+00f 5.00e-01f
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
double fpow(double base,int expt){
double ans = 1.0,temp = base;
if(base == 0)
return 0;
while(expt){
if(expt % 2 == 1)
ans = ans * temp;
temp = temp * temp;
expt /= 2;
}
return ans;
}
int main(int argc, char const *argv[])
{
int n,expt;
double base,ans;
while(scanf("%d",&n) != EOF){
while(n--){
scanf("%lf %d",&base,&expt);
if(expt < 0){
ans = fpow(base,0 - expt);
if(ans != 0)
ans = 1.0 / ans;
}
else
ans = fpow(base,expt);
if(ans == 0 && expt < 0)
printf("INF\n");
else
printf("%.2ef\n",ans);
}
}
return 0;
}#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <set>
#include <map>
#include <queue>
#include <stdlib.h>
using namespace std;
bool dblcmp(double x) {
if(x < 0) x = -x;
if(x <= 1e-10) return true;
return false;
}
int main(){
int t;
//freopen("1514_5.in", "r", stdin);
//freopen("5.out", "w", stdout);
scanf("%d",&t);
while(t--){
double base;
int exponent;
scanf("%lf %d",&base,&exponent);
double res = 1.0;
if(dblcmp(base)==true && exponent < 0) {
printf("INF\n");
continue;
}
int f = 0;
if(exponent < 0) {
f = 1;exponent = -exponent;
}
for(int i = 1;i <= exponent;i++){
res = res * base;
}
if(f==1) res = 1.0/res;
printf("%.2ef\n",res);
}
return 0;
}
本文介绍了一种高效计算指数运算的方法——快速幂算法,并通过具体的代码示例展示了如何使用该算法来处理不同情况下的指数计算问题。
410

被折叠的 条评论
为什么被折叠?



