输入一个非负整数n,生成一张3的乘方表,输出3^0~3^n的值。可调用幂函数计算3的乘方。
输入格式:
输入在一行中给出一个非负整数n。
输出格式:
按照幂的递增顺序输出n+1行,每行格式为“pow(3,i) = 3的i次幂的值”。题目保证输出数据不超过长整型整数的范围。
输入样例:
3
输出样例:
pow(3,0) = 1
pow(3,1) = 3
pow(3,2) = 9
pow(3,3) = 27
# include <stdio.h>
# include <math.h> //或者# include <cmath>
int main()
{
int n, i;
printf("计算3的乘方表,请输入非负整数n:");
scanf_s("%d", &n);
for (i = 0; i <= n; i++)
{
//使用pow函数之前要先include相关头文件 # include <math.h>或 #include <cmath>
//因为pow函数的原型为double pow(double x, double y),
//所以pow( )函数的返回值为double类型,故printf打印格式为%f或%lf,
//或者像示例一样先转为int类型,否则将输出0。
printf("pow(3,%d)=%d\n", i, (int)pow(3, i));
//printf("pow(3,%d)=%f\n", i, pow(3, i));
}
return 0;
}