zoukankan      html  css  js  c++  java
  • SGU112 a^bb^a

    112. ab-ba

    time limit per test: 0.5 sec. 
    memory limit per test: 4096 KB

     

    You are given natural numbers a and b. Find ab-ba.

     

    Input

    Input contains numbers a and b (1≤a,b≤100).

     

    Output

    Write answer to output.

     

    Sample Input

    2 3
    

    Sample Output

    -1
    题解:高精度乘法和高精度减法。水水的。。。不过第五个数据WA了,不知道为什么,是用函数memcmp去判断两个数组所代表的数的大小,莫名其妙的出错了,然后果断自己写了个判断,然后提交上去就AC了。。。
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAXSN 1500
     4 long x[MAXSN],y[MAXSN],temp[MAXSN];
     5 long a,b;
     6 int main(void)
     7 {
     8     long i,j,s,r,len1,len2,len;
     9     scanf("%ld%ld",&a,&b);
    10     memset(x,0,sizeof(x));
    11     memset(y,0,sizeof(y));
    12     x[0]=1;
    13     for(i=1; i<=b; i++)
    14     {
    15         long c;
    16         c=0;
    17         for(j=0; j<MAXSN; j++)
    18         {
    19             s=x[j]*a+c;
    20             x[j]=s%10;
    21             c=s/10;
    22         }
    23     }
    24     y[0]=1;
    25     for(i=1; i<=a; i++)
    26     {
    27 
    28         long c;
    29         c=0;
    30         for(j=0; j<MAXSN; j++)
    31         {
    32             s=y[j]*b+c;
    33             y[j]=s%10;
    34             c=s/10;
    35         }
    36     }
    37     len1=MAXSN-1;
    38     while(x[len1]==0) len1--;
    39     len2=MAXSN-1;
    40     while(y[len2]==0) len2--;
    41     if(len1<len2) len=len2;
    42     else
    43         len=len1;
    44     if(len1!=len2)
    45         r=len1<len2?-1:1;
    46     else
    47     {
    48         for(i=len1; i>=0; i--)
    49             if(x[i]!=y[i])
    50             {
    51                 r=x[i]<y[i]?-1:1;
    52                 break;
    53             }
    54     }
    55     if(r<0)
    56     {
    57         printf("-");
    58         memcpy(temp,x,sizeof(x));
    59         memcpy(x,y,sizeof(y));
    60         memcpy(y,temp,sizeof(temp));
    61     }
    62 
    63     for(i=0; i<=len; i++)
    64     {
    65         x[i]=x[i]-y[i];
    66         x[i+1]=x[i+1]+(x[i]>=0?1:0)-1;
    67         x[i]=x[i]+(x[i]>=0?0:1)*10;
    68     }
    69     while(x[len]==0&&len>=1) len--;
    70     for(i=len; i>=0; i--)
    71         printf("%ld",x[i]);
    72     printf("\n");
    73     return 0;
    74 }
     
  • 相关阅读:
    13.numpy线性代数和绘图
    12-numpy矩阵
    11-numpy视图与副本
    10-numpy排序搜索
    day12 异常 模块 单例
    day11面向对象 多态 静态方法 (三)
    day 10 面向对象(=)
    day9 面向对象
    day8 文件
    day7 地址 名片管理系统
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2950636.html
Copyright © 2011-2022 走看看