首页 技术 正文
技术 2022年11月9日
0 收藏 930 点赞 3,654 浏览 1995 个字

练练

大数加法一般为小学生式的“竖式计算”要特别注意的是借位与进位的问题(先给看c++写法,我怕先看了python写法,会看不下去c++写法)这题还有要注意的是

1、同符号的话,直接加就行,最后再看正负号

2、不同的话则看,两个数的模的大小,在最后判断填补正负号,

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 10110;
char ch1[maxn],ch2[maxn];
int a[maxn],b[maxn],p,q,carry;
int c[maxn],car;
bool flag;void add(int len){//carry=0无进位,=1有进位
for(int i=0;i<len;i++){
int t=a[i]+b[i]+carry;
if(t>=10){
carry=1;
c[i]=t-10;
}
else {
carry=0;
c[i]=t;
}
}
if(carry==1)c[len]=1;
}int cmp(int l){
for(int i=l-1;i>=0;i--){
if(a[i]==b[i])continue;
if(a[i]>b[i]){
return 1;
}
else {
return -1;
}
}
return 2;
}int minu(int l1,int l2){//car=0无借位,=1有借位
if(l1==l2){
int r=cmp(l1);
if(r==2){
flag=true;
return 0;
}
else if(r==1){
for(int i=0;i<l1;i++){
a[i]-=car;
if(a[i]>=b[i]){
c[i]=a[i]-b[i];
car=0;
}
else{
c[i]=a[i]+10-b[i];
car=1;
}
}
}
else {
for(int i=0;i<l2;i++){
b[i]-=car;
if(b[i]>=a[i]){
c[i]=b[i]-a[i];
car=0;
}
else{
c[i]=b[i]+10-a[i];
car=1;
}
}
}
}
else {
if(l1>l2){
for(int i=0;i<l1;i++){
a[i]-=car;
if(a[i]>=b[i]){
c[i]=a[i]-b[i];
car=0;
}
else{
c[i]=a[i]+10-b[i];
car=1;
}
}
}
else {
for(int i=0;i<l2;i++){
b[i]-=car;
if(b[i]>=a[i]){
c[i]=b[i]-a[i];
car=0;
}
else{
c[i]=b[i]+10-a[i];
car=1;
}
}
}
}
}int main(){
int len1=0,len2=0,i,j;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
cin>>ch1>>ch2;
if(ch1[0]=='-')p=1;
if(ch2[0]=='-')q=1;
for(i=strlen(ch1)-1;i>=p;i--){
a[len1++]=ch1[i]-'0';
}
for(i=strlen(ch2)-1;i>=q;i--){
b[len2++]=ch2[i]-'0';
}
carry=0;
car=0;
flag=false;
int len=max(len1,len2);
if(p==0&&q==0){
add(len);
}
else if(p==1&&q==1){
add(len);
cout<<"-";
}
else {
minu(len1,len2);
if(len1>len2&&p==1&&q==0){
cout<<"-";
}
else if(len1<len2&&q==1&&p==0){
cout<<"-";
}
else if(len1==len2&&cmp(len1)==1&&p==1&&q==0){
cout<<"-";
}
else if(len1==len2&&cmp(len1)==-1&&p==0&&q==1){
cout<<"-";
}
}
if(ch1[0]=='0'&&ch2[0]=='0'){
cout<<"0"<<endl;
return 0;
}
if(flag){
cout<<"0"<<endl;return 0;
}
if(!carry)len--;
while(len>=0){
if(c[len]!=0)break;
len--;
}//cout<<len<<endl;
while(len>=0){
cout<<c[len];
len--;
}cout<<endl;
return 0;
}

上面写的可能有点烦,还没来的及精简,先看着

下面是python

a=int (input());
b=int (input());
print(a+b);

是不是感觉被耍了,嘿嘿

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902