基准时间限制: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 c++代码
1 #include <iostream> 2 #include <cstring> 3 #include <bits/stdc++.h> 4 using namespace std; 5 6 string bigadd(string s,string ss){ 7 string tt; 8 int slen = s.length(); 9 int sslen = ss.length(); 10 reverse(s.begin(),s.end()); 11 reverse(ss.begin(),ss.end()); 12 int index = 0; 13 for(int i=0;i<min(slen,sslen);i++){ 14 int a = s[i]-‘0‘,b = ss[i]-‘0‘; 15 int at = a+b+index; 16 if(at>9){ 17 tt+=at-10+‘0‘; 18 index = 1; 19 }else{ 20 tt+=at+‘0‘; 21 index = 0; 22 } 23 } 24 for(int i=min(slen,sslen);i<max(slen,sslen);i++){ 25 if(slen>i){ 26 int a = s[i]-‘0‘; 27 int at = a+index; 28 if(at>9){ 29 tt+=at-10+‘0‘; 30 index = 1; 31 }else{ 32 tt+=at+‘0‘; 33 index = 0; 34 } 35 }else{ 36 int b = ss[i]-‘0‘; 37 int at = b+index; 38 if(at>9){ 39 tt+=at-10+‘0‘; 40 index = 1; 41 }else{ 42 tt+=at+‘0‘; 43 index = 0; 44 } 45 } 46 } 47 if(index){ 48 tt+=‘1‘; 49 } 50 reverse(tt.begin(),tt.end()); 51 return tt; 52 } 53 54 string bigdel(string s,string ss){//s>=ss 55 string tt,st; 56 int slen = s.length(); 57 int sslen = ss.length(); 58 reverse(s.begin(),s.end()); 59 reverse(ss.begin(),ss.end()); 60 int index = 0; 61 for(int i=0;i<sslen;i++){ 62 int a = s[i]-‘0‘; 63 int b = ss[i]-‘0‘; 64 int at = a-b-index; 65 if(at<0){ 66 tt+=at+10+‘0‘; 67 index = 1; 68 }else{ 69 tt+=at+‘0‘; 70 index = 0; 71 } 72 } 73 for(int i=sslen;i<slen;i++){ 74 int b = s[i]-‘0‘; 75 int at = b - index; 76 if(at<0){ 77 tt+=at+10+‘0‘; 78 index = 1; 79 }else{ 80 tt+=at+‘0‘; 81 index = 0; 82 } 83 } 84 bool prime = true; 85 for(int i = slen-1;i>=0;i--){ 86 if(tt[i]!=‘0‘){ 87 prime = false; 88 } 89 if(!prime){ 90 st+=tt[i]; 91 } 92 } 93 if(st.length()==0) 94 st+=‘0‘; 95 return st; 96 } 97 98 int compare(string s,string ss){ 99 int slen = s.length(); 100 int sslen = ss.length(); 101 if(slen==sslen){ 102 for(int i=0;i<slen;i++){ 103 if(s[i]==ss[i]){ 104 continue; 105 }else{ 106 return s[i]>ss[i]; 107 } 108 } 109 }else{ 110 return slen > sslen; 111 } 112 return 2; 113 } 114 string s,ss; 115 int main(){ 116 while(cin>>s>>ss){ 117 bool sflag=true, ssflag=true; 118 if(s[0]==‘-‘){ 119 s.erase(0,1); 120 sflag = false; 121 } 122 if(ss[0]==‘-‘){ 123 ss.erase(0,1); 124 ssflag = false; 125 } 126 if((sflag&&ssflag)||(!sflag&&!ssflag)){ 127 string st = bigadd(s,ss); 128 if(sflag) 129 cout<<st<<endl; 130 else{ 131 st=‘-‘+st; 132 cout<<st<<endl; 133 } 134 }else{ 135 string st; 136 if(sflag){//s为正数 137 if(compare(s,ss)==0){ 138 st = bigdel(ss,s); 139 st=‘-‘+st; 140 cout<<st<<endl; 141 }else if(compare(s,ss)==1){ 142 st = bigdel(s,ss); 143 cout<<st<<endl; 144 }else{ 145 cout<<"0"<<endl; 146 } 147 }else{//ss为正数 148 if(compare(s,ss)==0){ 149 st = bigdel(ss,s); 150 cout<<st<<endl; 151 }else if(compare(s,ss)==1){ 152 st = bigdel(s,ss); 153 st=‘-‘+st; 154 cout<<st<<endl; 155 }else{ 156 cout<<"0"<<endl; 157 } 158 } 159 } 160 } 161 return 0; 162 }
JAVA:
1 import java.util.*; 2 import java.io.*; 3 import java.lang.String; 4 import java.math.BigDecimal; 5 6 public class p1036 7 { 8 public static void main(String[] args) 9 { 10 String s1,s2; 11 Scanner cin = new Scanner(System.in); 12 s1 = cin.next(); 13 s2 = cin.next(); 14 BigDecimal b1 = new BigDecimal(s1); 15 BigDecimal b2 = new BigDecimal(s2); 16 System.out.println(b1.add(b2)); 17 cin.close(); 18 } 19 }
原文地址:https://www.cnblogs.com/zllwxm123/p/8945225.html
时间: 2024-10-12 22:17:30