码迷,mamicode.com
首页 > 其他好文 > 详细

大数求幂

时间:2020-03-21 14:51:45      阅读:41      评论:0      收藏:0      [点我收藏+]

标签:循环   while   pow   数组   pre   printf   power   include   type   

#include <stdio.h>
int main()
{
    int a[20001];//储存每一位所得到的数
    int temp,base,digit,power,i,j=0;//temp每次的得数   digit每次得数的位数  power是幂指数,base是底数
    printf("type the base");
    scanf("%d",&base);
    printf("type the power");
    scanf("%d",&power);
    a[0]=1;//从1开始乘
    digit=1;//位数从第一位开始
    for(i=1;i<=power;i++)
    {
        int num=0;//储存进位数
        for(j=0;j<digit;j++)
        {
            temp=a[j]*base+num;//将一个数的每一位数都分别乘以base,
            a[j]=temp%10;//将一个数的每一位数利用数组进行储存
            num=temp/10;
        }
        while(num)//判断退出循环后,num的值是否为0
        {
            a[digit]=num%10;//继续储存
            num=num/10;
            digit++;
        }
    }
    for(i=digit-1;i>=0;i--)//倒序输出每一位
        printf("%d",a[i]);
    printf("\n");
    return 0;
}

 

大数求幂

标签:循环   while   pow   数组   pre   printf   power   include   type   

原文地址:https://www.cnblogs.com/jeseesmith/p/12539174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!