大数模版

A+B
#include <stdio.h>
#include <string.h>
#define MAX_LEN 200
int an1[MAX_LEN+10];
int an2[MAX_LEN+10];
char szLine1[MAX_LEN+10];
char szLine2[MAX_LEN+10];
int nLen1; 
int nLen2;
int main(void)
{
    scanf("%s", szLine1);
    scanf("%s", szLine2);
    int i, j;
    memset( an1, 0, sizeof(an1));
   memset( an2, 0, sizeof(an2))
   nLen1 = strlen( szLine1);
    for( j = 0, i = nLen1 - 1;i >= 0 ; i --)
        an1[j++] = szLine1[i] - '0';
    nLen2 = strlen(szLine2);
    for( j = 0, i = nLen2 - 1;i >= 0 ; i --)
        an2[j++] = szLine2[i] - '0';
    for( i = 0;i < MAX_LEN ; i ++ ) 
   {  
     an1[i] += an2[i]; //逐位相加
        if( an1[i] >= 10 ) 
        { //看是否要进位
            an1[i] -= 10;
            an1[i+1] ++; //进位
        }
    }
    for( i = MAX_LEN; (i >= 0) && (an1[i] == 0); i -- ) ;		//消0
    	if(i>=0)
        	for( ; i >= 0; i--)
            	printf("%d", an1[i]);
    	else      				//如果结果为0
  printf("0");
    return 0;
}

A*B
#include <stdio.h>
#include <string.h>
#define MAX_LEN 200
int main()
{
    int i, j;
    int len1,len2;
    int a[MAX_LEN+10],b[MAX_LEN+10],c[MAX_LEN*2+10];
    char str1[MAX_LEN+10],str2[MAX_LEN+10];

    for(i=0;i<MAX_LEN+10;i++)  a[i]=b[i]=0;
    for(i=0;i<MAX_LEN*2+10;i++)  c[i]=0;
    gets(str1); //按字符串形式读入第一个整数
    gets(str2);
    len1=strlen(str1);
    for(j=0,i=len1-1; i>=0; i--)//把数字倒过来
        a[j++]=str1[i]-'0';
    len2=strlen(str2);
    for(j=0,i=len2-1; i>=0; i--)//倒转第二个整数
        b[j++]=str2[i]-'0';
for(i=0; i<len2; i++)//用第二个数乘以第一个数,每次一位 
    {
        for(j=0; j<len1; j++)
            c[i+j]+= b[i]*a[j]; //先乘起来,后面统一进位
    }
    for(i=0; i<MAX_LEN*2; i++)//循环统一处理进位问题 
    {
        if(c[i]>=10) 
        {
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
    }
    for(i=MAX_LEN*2; (c[i]==0)&&(i>=0); i--);//跳过高位的0
    if(i>=0)
        for(;i>=0;i--)
            printf("%d", c[i]);
    else
        printf("0");
    return 0;
}
A/B
#include <stdio.h>
#include <string.h>
#define MAX_LEN 200
char szLine1[MAX_LEN + 10];
char szLine2[MAX_LEN + 10];
int an1[MAX_LEN + 10]; //被除数, an1[0]对应于个位
int an2[MAX_LEN + 10]; //除数, an2[0]对应于个位
int aResult[MAX_LEN + 10]; //存放商,aResult[0]对应于个位
//长度为 nLen1 的大整数p1 减去长度为nLen2 的大整数p2
//结果放在p1 里,返回值代表结果的长度
//如不够减返回-1,正好减完返回 0
int Substract( int * p1, int * p2, int nLen1, int nLen2)
{
    int i;
    if( nLen1 < nLen2 )
        return -1;
//下面判断p1 是否比p2 大,如果不是,返回-1
   if( nLen1 == nLen2 ) 
    {
        for( i = nLen1-1; i>=0; i -- ) 
        {
            if( p1[i] > p2[i] )     
      break; //p1>p2
           else if( p1[i] < p2[i] ) 
  return -1; //p1<p2
       }
    }
    for( i = 0; i < nLen1; i ++ ) 
    { //要求调用本函数确保当i>=nLen2 时,p2[i] = 0
        p1[i] -= p2[i]; 
        if( p1[i] < 0 ) 
        {
            p1[i]+=10;
            p1[i+1] --;
        }
    }
    for( i = nLen1 -1 ; i >= 0 ; i-- )
        if( p1[i] )//找到最高位第一个不为0
            return i + 1;
    return 0;//全部为0,说明两者相等
}
int main()
{
    int t, n;
    scanf("%d", &n);
    for( t = 0; t < n; t ++ ) 
    {
        scanf("%s", szLine1);
        scanf("%s", szLine2);
        int i, j;
        int nLen1 = strlen( szLine1);
        memset( an1, 0, sizeof(an1));
        memset( an2, 0, sizeof(an2));
        memset( aResult, 0, sizeof(aResult));
        for( j = 0, i = nLen1 - 1;i >= 0 ; i --)
            an1[j++] = szLine1[i] - '0';
        int nLen2 = strlen(szLine2);
        for( j = 0, i = nLen2 - 1;i >= 0 ; i --)
            an2[j++] = szLine2[i] - '0';
        if( nLen1 < nLen2 ) 
        {
            printf("0\n");
            continue;
        }
int nTimes = nLen1 - nLen2;
        if(nTimes > 0)
        {
            for( i = nLen1 -1; i >= nTimes; i -- ) 
                an2[i] = an2[i-nTimes];//朝高位移动
            for( ; i >= 0; i--)//低位补0
                an2[i] = 0;
            nLen2 = nLen1;
        }
        for( j = 0 ; j <= nTimes; j ++ ) 
        {
            int nTmp;
            //一直减到不够减为止
            //先减去若干个 an2×(10 的 nTimes 次方),
           //不够减了,再减去若干个 an2×(10 的 nTimes-1 次方),......
       while( (nTmp = Substract(an1, an2+j, nLen1, nLen2-j)) >= 0) 
            {
               nLen1 = nTmp;
                aResult[nTimes-j]++; //每成功减一次,则将商的相应位加1
            }
        }
//下面输出结果,先跳过高位0
        for( i = MAX_LEN ; (i >= 0) && (aResult[i] == 0); i -- );
        if( i >= 0)
            for( ; i>=0; i--)
                printf("%d", aResult[i]);
        else
            printf("0");
        printf("\n");
    }
    return 0;
}



输入数据
只包含一个整数P(1000<P<3100000)
输出要求
第1 行:十进制高精度数2^p-1 的位数。 第2-11 行:十进制高精度数2^p-1 的最后500位数字。(每行输出50 位,共输出10 行,不足500 位时高位补0)
不必验证2^p-1 与P 是否为素数。
输入样例
1279
输出样例

386
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000104079321946643990819252403273640855
38615262247266704805319112350403608059673360298012
23944173232418484242161395428100779138356624832346
49081399066056773207629241295093892203457731833496
61583550472959420547689811211693677147548478866962
50138443826029173234888531116082853841658502825560
46662248318909188018470682222031405210266984354887
32958028878050869736186900714720710555703168729087
#include <stdio.h>
#include <memory.h>
#define LEN 125 //每数组元素存放4 位,数组最多需要125 个元素
#include <math.h>
//计算高精度乘法 a * b, 结果的末500 位放在a 中
void Multiply(int* a, int* b)
{
    int i, j;
    int nCarry; //存放进位
    int nTmp;
   int c[LEN]; //存放结果的末500 位
    memset(c, 0, sizeof(int) * LEN);
    for (i=0;i<LEN;i++) 
    {
        nCarry=0;
        for (j=0;j<LEN-i;j++) 
        {
           nTmp=c[i+j]+a[j]*b[i]+nCarry;
            c[i+j]=nTmp%10000;
           nCarry=nTmp/10000;
        }
    }
    memcpy( a, c, LEN*sizeof(int));
}
int main(void)
{
    int i;
    int p;
    int anPow[LEN]; //存放不断增长的2 的次幂
    int aResult[LEN]; //存放最终结果的末500 位
    scanf("%d", & p);
    printf(“%d\n”, (int)(p*log10(2.0))+1);
    //下面将2 的次幂初始化为2^(2^0)(a^b 表示a 的b 次方),
    // 最终结果初始化为1
    anPow[0]=2;
    aResult[0]=1;
    for (i=1;i<LEN;i++) 
    {
        anPow[i]=0;
        aResult[i]=0;
    }    
    while (p>0) //下面计算2 的p 次方
    { // p = 0 则说明p 中的有效位都用过了,不需再算下去
        if ( p & 1 ) //判断此时p 中最低位是否为1
            Multiply(aResult, anPow);
        p>>=1;
        Multiply(anPow, anPow);
   }
aResult[0]--; //2^p-1
    //输出结果
    for (i=LEN-1;i>=0;i--) 
    {
        if (i%25==12)
            printf("%02d\n%02d", aResult[i]/100,
            aResult[i]%100);
        else 
        {
            printf("%04d", aResult[i]);
            if (i%25==0)
                printf("\n");
        }
    }
    return 0;
}
   



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值