Experiment 2-4-2 Generate a power table of 3 (15 points)

Enter a non-negative integer n, generate a table of powers of 3, and output 3 0 ​ ​ — — 3 ​ n 3^{0}​​ ——3^{​n}303n values. The power function can be called to calculate the power of 3.

Input format:

Input gives a non-negative integer in one line n.

Output format:

Output n+1lines in the order of increasing power , and the format of each line is " pow(3,i) = 3的i次幂的值". The title guarantees that the output data does not exceed the range of a long integer.

Input sample:

3

Sample output:

pow(3,0) = 1
pow(3,1) = 3
pow(3,2) = 9
pow(3,3) = 27

Code:

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

int main() {
    
    
    int i,n;
    scanf("%d",&n); 
    long long value;
    for (i=0;i<=n;i++) {
    
    
        // pow()返回的类型是double
        value = pow(3,i);
        printf("pow(3,%d) = %lld\n",i,value);
    }
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

There are two details to pay attention to:

  • The title clearly indicates that the output data is in the range of long integers, so we can define a long long inttype of variable valueto receive the value of the exponentiation, and the formatting symbol for the final output is%lld
  • pow()The returned data is of doubletype, so it is inevitable that you need to force a replacement to lldform

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114433983