N的M次方,N和M都是超级大数,的后六位求法

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void fun(const char *a,const char *b ,char *c)
{
    
    int remainder=0;
    int length=strlen(a)>strlen(b)?strlen(a):strlen(b);
    int x=length;
    for (int i=strlen(a)-1,j=strlen(b)-1;i>=0||j>=0;i--,j--)
    {
        int x1,x2;
        if (i>=0) 
            x1=a[i]-'0';
        else
            x1=0;
        if (j>=0) 
            x2=b[j]-'0';
        else
            x2=0;
        div_t temp=div(x1+x2+remainder,10);
        remainder=temp.quot;
        c[length--]=temp.rem+'0';
        
    }    
    if(remainder!=0)
    {
        c[0]=remainder+'0';
        c[x+1]='\0';
    }
    else
    {
        for (int i=0;i<=x+1;i++)
        {
            c[i]=c[i+1];
        }
        c[x]='\0';
    }
}
void main()
{
    
    char sum[100000]={"\0"};
    char temp1[100000]={'\0'};
    char temp2[100000]={'\0'};
    sum[0]='1';
    for (int i=1;i<=722;i++)
    {
        if (strlen(sum)>6)
        {
            strcpy(temp2,sum+strlen(sum)-6);
            strcpy(temp1,sum+strlen(sum)-6);
        }
        else
        {
            strcpy(temp2,sum);
            strcpy(temp1,sum);
        }
        
        for (int j=1;j<=14694;j++)
        {
            fun(temp1,temp2,sum);
            if (strlen(sum)>6)
            {
                strcpy(temp1,sum+strlen(sum)-6);
            }
            else
            {
                strcpy(temp1,sum);
            }
        }
        
    }
    
    cout<<sum<<endl;
    
}

#include<stdio.h>  
int main()  
{  
    int i,x,y,last= 1; /*变量last保存求X的Y次方过程中的部分乘积的后三位*/  
    printf("Input X and Y(X**Y):");  
    scanf("%d**%d",&x,&y);  
    for(i = 1;i <= y;i++) /*X自乘Y次*/  
        last = last * x % 1000; /*将last乘X后对1000取模,即求积的后三位*/  
    printf("The last 3 digits of %d**%d is:%d\n",x,y,last%1000); /*打印结果*/  
    return 0;  
} 
 
  

 

 

转载于:https://www.cnblogs.com/GoAhead/archive/2012/09/12/2682351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值