zoukankan      html  css  js  c++  java
  • hdu 1002 A+B

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1002

    复习一下大数

    模板:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <math.h>
     5 #include <assert.h>
     6 #include <ctype.h>
     7 #include <map>
     8 #include <string>
     9 #include <set>
    10 #include <bitset>
    11 #include <utility>
    12 #include <algorithm>
    13 #include <vector>
    14 #include <stack>
    15 #include <queue>
    16 #include <iostream>
    17 #include <fstream>
    18 #include <list>
    19 using  namespace  std;
    20 
    21 const  int MAXL = 1005;
    22 struct  BigNum
    23 {
    24     int  num[MAXL];
    25     int  len;
    26     BigNum()
    27     {
    28         memset(num,0,sizeof(num));
    29     }
    30 };
    31 
    32 //高精度加法
    33 BigNum  Add(BigNum &a, BigNum &b)
    34 {
    35     BigNum c;
    36     int  i, len;
    37     len = (a.len > b.len) ? a.len : b.len;
    38     memset(c.num, 0, sizeof(c.num));
    39     for(i = 0; i < len; i++)
    40     {
    41         c.num[i] += (a.num[i]+b.num[i]);
    42         if(c.num[i] >= 10)
    43         {
    44             c.num[i+1]++;
    45             c.num[i] -= 10;
    46         }
    47     }
    48     if(c.num[len])
    49         len++;
    50     c.len = len;
    51     return  c;
    52 }
    53 void  print(BigNum &a)   //输出大数
    54 {
    55     int  i;
    56     for(i = a.len-1; i >= 0; i--)
    57         printf("%d", a.num[i]);
    58     printf("
    ");
    59 }
    60 
    61 
    62 void Init(BigNum &a, char *s, int &tag)   //将字符串转化为大数
    63 {
    64     int  i = 0, j = strlen(s);
    65     if(s[0] == '-')
    66     {
    67         j--;
    68         i++;
    69         tag *= -1;
    70     }
    71     a.len = j;
    72     for(; s[i] != ''; i++, j--)
    73         a.num[j-1] = s[i]-'0';
    74 }
    75 
    76 int main(void)
    77 {
    78     //freopen("in.txt","r",stdin);
    79     int T;
    80     char  s1[MAXL], s2[MAXL];
    81     scanf("%d",&T);
    82     for(int t=1;t<=T;t++)
    83     {
    84         BigNum a,b,c;
    85         memset(s1,0,sizeof(s1));
    86         memset(s2,0,sizeof(s2));
    87         scanf("%s %s",s1,s2);
    88         int tag=1;
    89         Init(a,s1,tag);
    90         Init(b,s2,tag);
    91         c=Add(a,b);
    92         if(t!=1)
    93         printf("
    ");
    94         printf("Case %d:
    ",t);
    95         printf("%s + %s = ",s1,s2);
    96         print(c);
    97     }
    98     return 0;
    99 }

    普通:

     1 #include<iostream>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<stdlib.h>
     5 #include<stdio.h>
     6 using namespace std;
     7 const int M=1005;
     8 void xsone(int a[],int b[],int lenz)
     9 {
    10     for(int i=0;i<lenz;i++)
    11     a[i]=a[i]+b[i];
    12     int temp=0;
    13     for(int i=0;i<lenz+1;i++)
    14     {
    15         a[i]+=temp;
    16         temp=a[i]/10;
    17         a[i]%=10;
    18     }
    19     int i;
    20     for(i=lenz+1;i>=0;i--)
    21     if(a[i]!=0)
    22     break;
    23     for(;i>=0;i--)
    24     printf("%d",a[i]);
    25     printf("
    ");
    26 }
    27 int main()
    28 {
    29     //freopen("in.txt","r",stdin);
    30     int n;
    31     scanf("%d",&n);
    32     for(int i=1;i<=n;i++)
    33     {
    34         char sa[M],sb[M];
    35         memset(sa,0,sizeof(sa));
    36         memset(sb,0,sizeof(sb));
    37         scanf("%s %s",sa,sb);
    38         int lena = strlen(sa);
    39         int lenb = strlen(sb);
    40         int lenz = lena>lenb ?lena :lenb;
    41 
    42         int a[M],b[M];
    43         memset(a,0,sizeof(a));
    44         memset(b,0,sizeof(b));
    45         int j;
    46         for(j=0;j<lena;j++)
    47         a[lena-1-j]=sa[j]-'0';
    48         for(j=0;j<lenb;j++)
    49         b[lenb-1-j]=sb[j]-'0';
    50         printf("Case %d:
    ",i);
    51         printf("%s + %s = ",sa,sb);
    52         xsone(a,b,lenz);
    53         if(i!=n)
    54         printf("
    ");
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    python note 30 断点续传
    python note 29 线程创建
    python note 28 socketserver
    python note 27 粘包
    python note 26 socket
    python note 25 约束
    Sed 用法
    python note 24 反射
    python note 23 组合
    python note 22 面向对象成员
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4348440.html
Copyright © 2011-2022 走看看