51nod1005

article/2025/9/7 19:29:22
1005 大数加法
基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
给出2个大整数A,B,计算A+B的结果。
Input
第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)
Output
输出A + B
Input示例
68932147586
468711654886
Output示例
537643802472
相关问题
大数乘法 
0
 
大数开平方 
320
 
大数进制转换 
320
 
大数除法 
160
 
大数乘法 V2 
80

#include  <iostream>
#include  <algorithm>
#include  <cstdio>
#include  <string>
#include  <string.h>
#include  <cmath>
#include  <vector>
using  namespace  std ;
struct  BigInteger
{
static  const  int  BASE  =  10 ;
static  const  int  WIDTH  =  1 ;
vector < int >  s ;
bool  flag ;
BigInteger ( long  long  num  =  0 )  {  * this  =  num ;  }  // 
BigInteger  operator = ( long  long  num )
{  // 
         s . clear ( ) ;
         if  ( num  <  0 )
         {
              flag  =  0 ;
              num  =  - num ;
         }
         else
              flag  =  1 ;
         do
         {
              s . push_back ( num  %  BASE ) ;
              num / =  BASE ;
         }  while  ( num  >  0 ) ;
         return  * this ;
}
BigInteger  operator = ( const  string  & str )
{  // 
         s . clear ( ) ;
         if  ( str [ 0 ]  ==  '-' )
              flag  =  0 ;
         else
              flag  =  1 ;
         string  strr = str ;
         if  ( flag  ==  0 )
              strr . erase ( 0 ,  1 ) ;
         int  x ,  len  =  ( strr . length ( )  -  1 ) /  WIDTH  +  1 ;
         int  i  =  0 ;
         for  ( ;  i  <  len ;  i ++ )
         {
              int  end  =  strr . length ( )  -  i  *  WIDTH ;
              int  start  =  max ( 0 ,  end  -  WIDTH ) ;
              sscanf ( strr . substr ( start ,  end  -  start ) . c_str ( ) ,  "%d" ,  & x ) ;
              s . push_back ( x ) ;
         }
         return  * this ;
}
BigInteger  operator + ( const   BigInteger  & b )  const
{
         BigInteger  c , d ;
         if  ( b . flag  ==  0 )
         {
              BigInteger  B  =  b ;
              B . flag  =  1 ;
              if  ( flag  ==  0 )
              {
                   d  =  * this ;
                   d . flag  =  1 ;
                   c  =  d  +  B ;
                   c . flag  =  0 ;
                   return  c ;
              }
              return  * this  -  B ;
         }
         if  ( flag  ==  0 )
         {
              c  =  * this ;
              c . flag  =  1 ;
              return  b  -  c ;
         }
         c . s . clear ( ) ;
         for  ( int  i  =  0 ,  g  =  0 ;;  i ++ )
         {
              if  ( g  ==  0  &&  i  >=  s . size ( )  &&  i  >=  b . s . size ( ))
                   break ;
              int  x  =  g ;
              if  ( i  <  s . size ( ))
                   x  +=  s [ i ] ;
              if  ( i  <  b . s . size ( ))
                   x  +=  b . s [ i ] ;
              c . s . push_back ( x  %  BASE ) ;
              g  =  x /  BASE ;
         }
         return  c ;
}
BigInteger  operator * ( const  BigInteger  & b )  const
{
         BigInteger  c ,  t ;
         if  ( b . flag  ==  flag )
              c . flag  =  1 ;
         else
              c . flag  =  0 ;
         c  =  0 ;
         for  ( int  i  =  0 ;  i  <  s . size ( ) ;  i ++ )
         {
              t . s . clear ( ) ;
              long  long  x ,  g ;
              int  j ;
              j  =  i ;
              while  ( j -- )
                   t . s . push_back ( 0 ) ;
              for  ( j  =  g  =  0 ;;  j ++ )
              {
                   if  ( j  >=  b . s . size ( )  &&  g  ==  0 )
                        break ;
                   x  =  g ;
                   if  ( j  <  b . s . size ( ))
                        x  +=  s [ i ]  *  b . s [ j ] ;
                   t . s . push_back ( x  %  BASE ) ;
                   g  =  x /  BASE ;
              }
              c  +=  t ;
         }
         return  c ;
}
BigInteger  operator * ( const  int  & b )  const
{
         BigInteger  c  =  b ;
         return  c  *  ( * this ) ;
}
BigInteger  operator * ( const  long  long  int  & b )  const
{
         BigInteger  c  =  b ;
         return  c  *  ( * this ) ;
}
BigInteger  operator^ ( int  b )  const
{
         BigInteger  ans  =  1 ,  base  =  * this ;
         while  ( b )
         {
              if  ( b  &  1 )
              {
                   ans  =  base  *  ans ;
              }
              base  =  base  *  base ;
              b  >>=  1 ;
         }
         return  ans ;
}
BigInteger  operator += ( const  BigInteger  & b ) const
{
         return  * this + b ;
}
bool  operator < ( const  BigInteger  & b )  const
{
         if  ( b . flag  ==  0  &&  b . flag  !=  flag )
              return  false ;
         if  ( s . size ( )  !=  b . s . size ( ))
              return  s . size ( )  <  b . s . size ( ) ;
         for  ( int  i  =  s . size ( )  -  1 ;  i  >=  0 ;  i -- )
              if  ( s [ i ]  !=  b . s [ i ])
                   return  s [ i ]  <  b . s [ i ] ;
         return  false ;
}
bool  operator > ( const  BigInteger  & b )  const  {  return  b  <  * this ;  }
bool  operator <= ( const  BigInteger  & b )  const  {  return  ! ( b  <  * this ) ;  }
bool  operator >= ( const  BigInteger  & b )  const  {  return  ! ( * this  <  b ) ;  }
bool  operator != ( const  BigInteger  & b )  const  {  return  b  <  * this  ||  * this  <  b ;  }
bool  operator == ( const  BigInteger  & b )  const  {  return  ! ( b  <  * this )  &&  ! ( * this  <  b ) ;  }
BigInteger  operator - ( const  BigInteger  & b )  const
{
         if  ( b . flag  ==  0 )
         {
              BigInteger  B  =  b ;
              B . flag  =  1 ;
              return  * this  +  B ;
         }
         BigInteger  A ,  B ;
         A  =  * this ;
         B  =  b ;
         A . flag  =  1 ;
         B . flag  =  1 ;
         BigInteger  c ;
         if  ( A  <  B )
         {
              swap ( A ,  B ) ;  c . flag  =  0 ;
         }
         c . s . clear ( ) ;
         int  i ,  g ,  x ;
         i  =  g  =  0 ;
         while  ( 1 )  //for (int i = 0, g = 0;; i++)
         {
              if  ( i  >=  A . s . size ( )  &&  i  >=  B . s . size ( ))
                   break ;
              x  =  A . s [ i ] ;
              x  -=  g ;
              g  =  0 ;
              if  ( i  <  B . s . size ( ))
                   x  -=  B . s [ i ] ;
              if  ( x  <  0 )
              {
                   x  +=  10 ;
                   g  =  1 ;
              }
              c . s . push_back ( x ) ;
              i ++ ;
         }
         if  ( c . s . back ( )  ==  0 )
         {
              vector < int > :: iterator  it  =  c . s . end ( ) ;
              for  ( it -- ;  it  !=  c . s . begin ( ) ;  it -- )
              {
                   if  ( * it  !=  0 )
                        break ;
              }
              it ++ ;
              c . s . erase ( it ,  c . s . end ( )) ;
         }
         if  ( c . s . empty ( ))
              c . s . push_back ( 0 ) ;
         return  c ;
}
BigInteger  operator - ( const  int  & b )  const
{
         BigInteger  c  =  b ;
         return  * this  -  c ;
}
BigInteger  operator - ( const  long  long  int  & b )  const
{
         BigInteger  c  =  b ;
         return  * this  -  c ;
}
BigInteger  operator -= ( const  BigInteger  & b )  const  {  return  * this  -  b ;  }
BigInteger  operator ! ( void ) const  //
{
         BigInteger  ans  =  1 ;
         for  ( BigInteger  i  =  1 ;  i  <=  * this ;  i  +=  1 )
         {
              ans  =  i  *  ans ;
         }
         return  ans ;
}
BigInteger  operator/ ( const  BigInteger  & b )  const
{
         //A/B
         BigInteger  A ,  B ,  c ,  ans ;
         if  ( b . flag  ==  flag )
              ans . flag  =  1 ;
         else
              ans . flag  =  0 ;
         if  ( b  >  * this )
              return  0 ;
         if  ( b  ==  * this )
              return  1 ;
         c . s . clear ( ) ;
         ans . s . clear ( ) ;
         A  =  * this ;
         B  =  b ;
         A . flag  =  B . flag  =  c . flag  =  1 ;
         int  zero  =  ( A . s . size ( )  -  b . s . size ( )) ;
         B . s . insert ( B . s . begin ( ) ,  zero ,  0 ) ;
         while  ( ! B . s . empty ( )  &&  B  >=  b )
         {
              int  x  =  0 ;
              while  ( A  >=  B )
              {
                   A  =  A  -  B ;
                   x ++ ;
              }
              c . s . push_back ( x ) ;
              B . s . erase ( B . s . begin ( )) ;
         }
         if  ( ! c . s . empty ( ))
              for  ( vector < int > :: reverse_iterator  it  =  c . s . rbegin ( ) ;  it  !=  c . s . rend ( ) ;  it ++ )
              {
                   ans . s . push_back ( * it ) ;
              }
         else
              ans . s . push_back ( 0 ) ;
         if  ( ans . s . empty ( ))
              ans . s . push_back ( 0 ) ;
         if  ( ans . s . back ( )  ==  0 )
         {
              vector < int > :: iterator  it  =  ans . s . end ( ) ;
              for  ( it -- ;  it  !=  ans . s . begin ( ) ;  it -- )
              {
                   if  ( * it  !=  0 )
                        break ;
              }
              it ++ ;
              ans . s . erase ( it ,  ans . s . end ( )) ;
         }
         return  ans ;
}
} ;
ostream  & operator << ( ostream  & out ,  const  BigInteger  & x )
{
if  ( x . flag  ==  0 )
         out  <<  '-' ;
out  <<  x . s . back ( ) ;
for  ( int  i  =  x . s . size ( )  -  2 ;  i  >=  0 ;  i -- )
{
         char  buf [ 20 ] ;
         sprintf ( buf ,  "%d" ,  x . s [ i ]) ;
         for  ( int  j  =  0 ;  j  <  strlen ( buf ) ;  j ++ )
              out  <<  buf [ j ] ;
}
return  out ;
}
istream  & operator >> ( istream  & in ,  BigInteger  & x )
{
string  s ;
if  ( ! ( in  >>  s ))
         return  in ;
x  =  s ;
return  in ;
}
int  main ( )
{
BigInteger  a ,  b ;
cin  >>  a  >>  b ;
cout  <<  a  +  b  <<  endl ;
}
运行代码
 
提交代码
0
分值
4924
提交
1381
AC
题目描述
所有提交记录
我的提交记录( 1)
排行榜
相关讨论
相关收藏
值得编写
leeeecx 创建(  1 人关注)
错题
slaughter。 创建(  0 人关注)
大数
SkyrimRaker 创建(  0 人关注)
BigInteger
Darkness 创建(  0 人关注)
1
22 天
7 小时
6 分钟
49 秒
2
24 天
0 小时
2 分钟
5 秒
3
41 天
8 小时
44 分钟
34 秒
AC了该问题的人

http://chatgpt.dhexx.cn/article/KOE5VPpl.shtml

相关文章

51nod---无法表示的数

题目&#xff1a;http://www.51nod.com/onlineJudge/questionCode.html#!problemId1176 题意&#xff1a;z (x/2)取整后 y xy,x,y都是大于0的整数。即&#xff1a; x&#xff0c;y取不同的数&#xff0c;z可能有多种表示方式&#xff0c;也可能一种都没有&#xff0c;比如3&…

51nod

1640 天气晴朗的魔法 题目来源&#xff1a; 原创 基准时间限制&#xff1a;1 秒 空间限制&#xff1a;131072 KB 分值: 20 难度&#xff1a;3级算法题 收藏 关注 这样阴沉的天气持续下去&#xff0c;我们不免担心起他的健康。 51nod魔法学校近日开展了主题为“天气晴朗”的魔法…

open3d剩余内容有关函数详情(笔记10)

本次笔记包括网格变形&#xff08;Mesh deformation&#xff09;、内在形状特征&#xff08;Intrinsic shape signatures&#xff09;ISS、光线投射&#xff08;Ray Casting&#xff09;和距离查询&#xff08;Distance Queries&#xff09;内容。 1. 网格变形&#xff08;Mes…

libigl第五章-参数化

在计算机图形学中&#xff0c;我们将表面参数化表示为从表面到 R 2 R^2 R2 。它通常由网格的每个顶点的一组新的 2D 坐标编码&#xff08;并且可能还通过与原始曲面的面一一对应的一组新面&#xff09;请注意&#xff0c;此定义与经典微分几何定义相反。 参数化有很多应用&…

libigl第四章-变形

现代基于网格的形状变形方法满足手柄&#xff08;网格上选定的顶点或区域&#xff09;处的用户变形约束&#xff0c;并将这些手柄变形平滑地传播到形状的其余部分&#xff0c;而不会删除或扭曲细节。Libigl 提供了各种最先进的变形技术的实现&#xff0c;从基于二次网格的能量最…

SAP License:ARAP知识点

总部&#xff08;Head Office/Branch&#xff09;。公司代码视图&#xff0d;帐户管理&#xff0d;会计信息中的总部字段建立客户与总部的关联&#xff0c;这样客户的发票或付款就可以自动地过帐到总部去。 Corporate group&#xff08;常规视图&#xff0d;控制数据&#xff…

非固定边界网格参数化(ARAP)

非固定边界的网格参数化方法。 记录自己的实现过程&#xff0c;一方面可能对其他人有用&#xff0c;另一方面自己保存。 1、首先实现固定边界的网格参数化方法&#xff0c;参考如下论文 M. Floater. Parameterization and smooth approximation ofsurface triangulations. CAG…

arap deformation 网格变形可视化

欢迎关注更多精彩 关注我&#xff0c;学习常用算法与数据结构&#xff0c;一题多解&#xff0c;降维打击。 rarp变形全称是 As-Rigid-As-Possible Suface Deformation. 意思是变形时尽量使每条边保持一个钢性变换。基本思路是基于能量优化来做。 能量定义 E ∑ i 1 N v w …

ARAP参数化算法

ARAP参数化算法实现 综述 三维模型的参数化把三维模型映射到二维平面&#xff0c;LSCM在映射的过程中尽可能地保持三角形的角度相同&#xff0c;ARAP参数化算法在LSCM的基础上尽可能的保证三角形没有扭曲地映射在二维平面上。 算法设计 因为需要映射过程中尽可能保持三角形…

经典论文推导: As-Rigid-As-Possible(ARAP) Surface Modeling

论文As-Rigid-As-Possible Surface Modeling 发表于SGP 2007&#xff0c;是变形领域的经典论文&#xff0c;目前引用已经超过1000次。网格变形要求产生视觉合理并且大致满足物理规律的变形效果&#xff0c;而模型细节的保持很大程度地满足了这种需求。刚性作为一个重要的属性在…

ARAP(As-Rigid-As-Possible)变形算法

上图中&#xff0c;最左侧的模型为其初始状态&#xff0c;由后面几种模型形状的变换我们可以发现它实际上就是要求变形前后模型每一个局部都只经历了平移或者旋转&#xff0c;也就是刚体变换&#xff08;rigid -transformation&#xff09;。我们知道&#xff0c;刚体变换是不会…

MiKTeX安装

MiKTeX安装&#xff08;导师使用这个版本&#xff09; 1&#xff0c;下载 2&#xff0c;打开文件 3&#xff0c;安装 安装好点击下一步&#xff0c; 二&#xff1a;选择并配置编辑器TeXstudio 1&#xff0c;下载地址&#xff1a;http://texstudio.sourceforge.net/ 错了…

LaTeX配置:MiKTeX+WinEdt

LaTeX(Tex)是一种语言&#xff0c;搭配上编译器(MIkTEX、TexLive)和编辑器(WinEdt、VS Code等)可以程序化生成PDF文档。本次给大家分享的是MiKTeXWinEdt的配置(安装方便快捷&#xff0c;占用空间小)。具体步骤如下&#xff1a; 预下载文件 下载Ctex2.9版本套装&#xff1a;http…

MikTex与Texlive 共存,vscode设置使用Texlive编译

为了在word中使用latex&#xff0c;安装了MikTex。 但在电脑上同时安装了MikTex 与Texlive之后&#xff0c;vscode本来之前默认用texlive编译项目&#xff0c;项目默认的编辑器变成了MikTex。 解决办法一&#xff1a; 一种说法是&#xff0c;在程序路径中删除MikTex的相关路径变…

MikTex+TexStudio配置

安装MikTex TexStudio cmd下mpm调出MikTex宏包管理器&#xff0c;下载宏包 相关参考&#xff1a; http://freshstu.com/2013/05/how-to-use-chinese-fonts-in-latax/ http://blog.sina.com.cn/s/blog_564b1b9b0100njfc.html http://guo-ch.blog.163.com/blog/static/120255…

【LaTeX】MiKTeX+TeXstudio安装过程

下载并安装MiKTeX 下载地址&#xff1a;MiKTeX 一路点击下一步即可 下载并安装TeXstudio 下载地址&#xff1a;TeXstudio 一路点击下一步即可 安装顺序一定是先MiKTeX&#xff0c;然后是TeXstudio&#xff0c;否则会报错 检查下配置 option->configure Texstudio-&g…

二、VSCode——MiKTeX编写latex编码

免安装下载VSCode https://blog.csdn.net/qq_40837795/article/details/128037675 下载MiKTeX https://miktex.org/download 配置MiKTeX https://blog.csdn.net/qq_40837795/article/details/120388489 配置VSCode LaTeX workshop 1、点击左侧Extensions&#xff0c;搜…

关于MikTex和TexStudio的安装

一位老师在课上向我们推荐使用了MikTex和TexStudio&#xff0c;要求我们提交pdf文档&#xff0c;以及学习一下新工具&#xff0c;这里记录一下这两个工具的安装过程。 1. Mik的下载与安装。 直接搜索MikTex&#xff0c;进入官方网址就可以下载。MikTex: https://miktex.org/ …

Texmaker+Miktex配置

Mixtex下载链接&#xff1a;https://miktex.org/download Texmaker下载链接&#xff1a;http://www.xm1math.net/texmaker/ 两者正常下载安装&#xff0c;记住Mixtex安装路径&#xff0c;主要是配置Texmaker 打开Texmaker&#xff0c;点击选项——配置Texmaker&#xff0c;出…