以字符串的形式读入两个数字,再以字符串的形式输出两个数字的和。
#include<iostream> (720)#include<string> #include<vector> using namespace std; // 两个字符串相加,模拟手算。两个字符串只有数字,没有符号 string addString(string str1,string str2){ string res=""; int pos1=str1.size()-1, pos2=str2.size()-1; int C=0, num1=0, num2=0, sum=0; // 从后向前逐个相加。 for(;pos1>=0||pos2>=0; pos1--, pos2--){ num1=pos1>=0? str1[pos1]-'0': 0; num2=pos2>=0? str2[pos2]-'0': 0; sum=num1+num2+C; res= to_string(sum%10) + res; C=sum/10; } if(C){ res= to_string(C)+res; } return res; } // 两个字符串相减,模拟手算。两个字符串只有数字,没有符号 string minusString(string str1, string str2){ bool res_positive=true; // 若str1表示的数字小于str2的,两者交换,并将结果符号设为负。 if(str1.size()<str2.size() || (str1.size()==str2.size()&& str1<str2)){ res_positive=false; string temp=str2; str2=str1; str1=temp; } string res=""; int pos1=str1.size()-1, pos2=str2.size()-1; int C=0, num1=0, num2=0; // 从后向前逐个相减 for(;pos1>=0||pos2>=0; pos1--, pos2--){ num1=pos1>=0? str1[pos1]-'0': 0; num2=pos2>=0? str2[pos2]-'0': 0; if(num1-C<num2){ num1= num1-C+10; C=1; }else{ num1=num1-C; C=0; } res= to_string(num1-num2) + res; } pos1=0; // 找到第一个非零位置 while(pos1<res.size()&& res[pos1]=='0'){ pos1++; } if(pos1==res.size()){ // res都是0 res="0"; }else if(pos1>0){ // res前缀部分是0 res=res.substr(pos1); } if(res_positive==false){ // 结果res是负数 res= "-"+res; } return res; } int main(){ string str1, str2; getline(cin, str1); getline(cin, str2); // 去掉引号 str1=str1.substr(1, str1.size()-2); str2=str2.substr(1, str2.size()-2); // 去掉正负号,记录数值正负 bool positive1=true, positive2=true; if(str1[0]=='-'){ positive1=false; str1=str1.substr(1); }else if(str1[0]=='+') str1=str1.substr(1); if(str2[0]=='-'){ positive2=false; str2=str2.substr(1); }else if(str2[0]=='+') str2=str2.substr(1); string res; // 这两个字符串异号 if(positive1 ^ positive2){ if(positive1==false){ // 第一个字符串是负数 res=minusString(str2, str1); }else { // 第二个字符串是负数 res=minusString(str1,str2); } }else{ // 同号 res=addString(str1,str2); if(positive1==false) res="-"+res; } cout<<"\"" <<res<<"\""<<endl; return 0; }
package main import ( "fmt" "strconv" ) func main() { var numOne,numTwo string fmt.Scanln(&numOne) fmt.Scanln(&numTwo) n, _ := strconv.ParseInt(numOne[1:len(numOne)-1], 10, 64) m, _ := strconv.ParseInt(numTwo[1:len(numOne)-1], 10, 64) fmt.Println("\"" + fmt.Sprintf("%d", m+n) + "\"") }golang的,大数写法不知道为啥没通过,然后直接int就行
//题目给出的范围不足以让两个int相加越界 import java.util.Scanner; /** * @ClassName Main * @Description TODO * @Author Wlison * @Date 2020/3/11 9:38 * @Version 1.0 **/ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String a = sc.nextLine(); String b = sc.nextLine(); String res ="\""+ (Integer.parseInt(a.substring(1,a.length()-1))+Integer.parseInt(b.substring(1,b.length()-1)))+"\""; System.out.println(res); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str1 = scanner.nextLine(); String str2 = scanner.nextLine(); //去除引号 str1 = str1.substring(1,str1.length()-1); str2 = str2.substring(1,str2.length()-1); //符号位 char str1D = str1.charAt(0); char str2D = str2.charAt(0); if (str1D!='-')str1D = '+'; if (str2D!='-')str2D = '+'; //位数 int str1L = str1.charAt(0)=='-'?str1.length()-1:str1.length(); int str2L = str2.charAt(0)=='-'?str2.length()-1:str2.length(); str1 = str1.charAt(0)=='-'?str1.substring(1,str1.length()):str1; str2 = str2.charAt(0)=='-'?str2.substring(1,str2.length()):str2; StringBuffer result = new StringBuffer(); //判断符号情况 if (str1D=='-'&&str2D=='-'){ int up = 0; int i=str1L,j=str2L; for (i=str1L-1,j=str2L-1;i>=0&&j>=0;i--,j--){ int temp = Integer.parseInt(String.valueOf(str1.charAt(i)))+Integer.parseInt(String.valueOf(str2.charAt(j)))+up; up = temp/10; result.append(temp%10); } //System.out.println("\""+result.reverse().toString()+"\""); while (i>=0){ int temp = Integer.parseInt(String.valueOf(str1.charAt(i)))+up; up = temp/10; result.append(temp%10); i--; } while (j>=0){ int temp = Integer.parseInt(String.valueOf(str2.charAt(j)))+up; up = temp/10; result.append(temp%10); j--; } if (up != 0)result.append(up); result.append('-'); System.out.println("\""+result.reverse().toString()+"\""); }else if ((str1D=='+'&&str2D=='-')||(str1D=='-'&&str2D=='+')){ int up =0; char maxD,minD; String maxstr,minstr; if (str1L>str2L){ maxstr = str1; maxD = str1D; minstr = str2; minD = str2D; }else if (str1L<str2L){ maxstr = str2; maxD = str2D; minstr = str1; minD = str1D; }else { if (str1.compareTo(str2)>0){ maxstr = str1; maxD = str1D; minstr = str2; minD = str2D; }else { maxstr = str2; maxD = str2D; minstr = str1; minD = str1D; } } int i=maxstr.length()-1,j=minstr.length()-1; for (;i>=0&j>=0;i--,j--){ int temp = Integer.parseInt(String.valueOf(maxstr.charAt(i)))-Integer.parseInt(String.valueOf(minstr.charAt(j)))+up; if (temp >=0){ result.append(temp); up = 0; }else { temp = temp+10; up = -1; result.append(temp); } } //System.out.println("\""+result.reverse().toString()+"\""); while (i>=0){ int temp = Integer.parseInt(String.valueOf(maxstr.charAt(i)))+up; if (temp >=0){ result.append(temp); up = 0; }else { temp = temp+10; up = -1; result.append(temp); } i--; } if (maxD == '-'){ result.append('-'); } System.out.println("\""+Integer.parseInt(result.reverse().toString())+"\""); }else { int up = 0; int i=str1L,j=str2L; for (i=str1L-1,j=str2L-1;i>=0&&j>=0;i--,j--){ int temp = Integer.parseInt(String.valueOf(str1.charAt(i)))+Integer.parseInt(String.valueOf(str2.charAt(j)))+up; up = temp/10; result.append(temp%10); } //System.out.println("\""+result.reverse().toString()+"\""); while (i>=0){ int temp = Integer.parseInt(String.valueOf(str1.charAt(i)))+up; up = temp/10; result.append(temp%10); i--; } while (j>=0){ int temp = Integer.parseInt(String.valueOf(str2.charAt(j)))+up; up = temp/10; result.append(temp%10); j--; } if (up != 0)result.append(up); System.out.println("\""+result.reverse().toString()+"\""); } } }
#include <iostream> #include <string> #include <algorithm> /** * 溢出:只有同号才会有可能是溢出,互异时直接转换为整数,相加,不会溢出。 */ void add(std::string x, std::string y) { int x_len = x.length(); int y_len = y.length(); if(x.front()=='-' && y.front() !='-' || x.front()!='-' && y.front() =='-') { // 直接相加 std::cout<<"\""<< (std::stod(x) + std::stod(y))<<"\""<<std::endl; return; } bool negative = x.front() =='-' ? true : false; std::string result; result.reserve(std::max(x_len, y_len)); int i=x_len-1, j=y_len-1; int carry=0; for(; (i >=0 && x[i] !='-') && (j>=0 && y[j] != '-'); --i, --j) { int num1 = x[i]-'0'; int num2 = y[j]-'0'; int sum = num1 + num2 + carry; carry = sum / 10; result.push_back(sum % 10 +'0'); } while(i >=0 && x[i] !='-') { int sum = x[i]-'0' +carry; carry = sum / 10; result.push_back((sum % 10) +'0'); --i; } while(j>=0 && y[j] != '-') { int sum = y[j]-'0' +carry; carry = sum / 10; result.push_back((sum % 10) +'0'); --j; } if(carry==1) { result.push_back('1'); } std::reverse(result.begin(), result.end()); std::cout<<"\""; if(negative) std::cout<<'-'; std::cout<<result; std::cout<<"\""; } int main(int argc, char* const argv[]) { std::string x, y; std::cin>>x>>y; add(x.substr(1, x.length()-2), y.substr(1, y.length()-2)); return 0; }
#include<string> (765)#include<iostream> using namespace std; string sum(string& num1, string& num2) { bool pos1 = num1[0] != '-'; bool pos2 = num2[0] != '-'; int op = (pos1 ^ pos2) ? -1 : 1; if (num1[0] == '-' || num1[0] == '+') num1 = num1.substr(1); if (num2[0] == '-' || num2[0] == '+') num2 = num2.substr(1); if (op == -1) { if (num1.size() < num2.size() || (num1.size() == num2.size() && num1 < num2)) { swap(num1, num2); swap(pos1, pos2); } } int index = 1; int carry=0; string res=""; while (index <= num1.size() || index <= num2.size()) { char c1 = '0', c2 = '0'; if (index <= num1.size()) c1 = num1[num1.size() - index]; if (index <= num2.size()) c2 = num2[num2.size() - index]; int tmp = (c1 - '0') + op * (c2 - '0') + carry; carry = 0; if (tmp < 0) { tmp += 10; carry = -1; } if (tmp >= 10) { tmp -= 10; carry = 1; } res = to_string(tmp) + res; ++index; } if (carry > 0) res = to_string(carry) + res; index = 0; while (index < res.size() && res[index] == '0')++index; if (index == res.size()) return "0"; res = res.substr(index); if (!pos1) res = '-' + res; return res; } int main() { string num1, num2; cin >> num1 >> num2; num1 = num1.substr(1, num1.size() - 2); num2 = num2.substr(1, num2.size() - 2); cout <<"\""<< sum(num1, num2) << "\"" << endl; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.next(); String s2 = in.next(); s1 = s1.substring(1,s1.length()-1); s2 = s2.substring(1,s2.length()-1); int num1 = Integer.valueOf(s1); int num2 = Integer.valueOf(s2); int sum = num1+num2; System.out.println("\"" +sum+ "\""); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); String num1=sc.next(); String num2=sc.next(); //判断是否为负数 int flag1=num1.charAt(1)=='-'?-1:1; int flag2=num2.charAt(1)=='-'?-1:1; int number1=0,number2=0; //逐一取出读取的字符,转化为数字,加上原来的数字乘以10 for(int i=0;i<num1.length();i++){ if(num1.charAt(i)!='\"'&&num1.charAt(i)!='-'){ number1=10*number1+num1.charAt(i)-'0'; } } for(int j=0;j<num2.length();j++){ if(num2.charAt(j)!='\"'&&num2.charAt(j)!='-'){ number2=10*number2+num2.charAt(j)-'0'; } } //是负数就乘以-1 number1=flag1==-1?number1*-1:number1; number2=flag2==-1?number2*-1:number2; System.out.println("\""+(number1+number2)+"\""); } }
import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); String s1=scanner.nextLine(); String s11=s1.substring(1,s1.length()-1); String s2 =scanner.nextLine(); String s22=s2.substring(1,s2.length()-1); if (s1.length()==0 || s2.length()==0) System.out.println(-1); long a=Long.parseLong(s11); long b=Long.parseLong(s22); long c=a+b; System.out.println("\""+c+"\""); } }
题有点坑 要去掉输入的引号
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); String num1 = input.nextLine(); num1=num1.substring(1,num1.length()-1); String num2 = input.nextLine(); num2=num2.substring(1,num2.length()-1); BigInteger add=new BigInteger(num1).add(new BigInteger(num2)); System.out.println("\""+add+"\""); } }