[编程题]A+B
  • 热度指数:11937 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。 现在请计算A+B的结果,并以正常形式输出。

输入描述:
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。


输出描述:
请计算A+B的结果,并以正常形式输出,每组数据占一行。
示例1

输入

-234,567,890 123,456,789
1,234 2,345,678

输出

-111111101
2346912
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //Java int 类整数的最大值是 2147483647 满足题目条件
        Scanner scanner = new Scanner(System.in);
        System.out.println(getNum(scanner.next())+getNum(scanner.next()));
    }
    
    public static int getNum(String s){
        String s1 = s.replace(",", "");
        return s1.startsWith("-")?-Integer.parseInt(s1.substring(1)):Integer.parseInt(s1);
    }
}


发表于 2020-03-17 10:03:45 回复(0)
import java.io.*;

public class Main{
    
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] line = reader.readLine().split(" ");
        String[] a = line[0].split(",");
        String[] b = line[1].split(",");
        StringBuffer aa = new StringBuffer();
        StringBuffer bb = new StringBuffer();
        for(int i = 0;i<a.length;i++) {
            aa.append(a[i]);
        }
        for(int i = 0;i<b.length;i++) {
            bb.append(b[i]);
        }
        
        int aval = Integer.parseInt(aa.toString());
        int bval = Integer.parseInt(bb.toString());
        System.out.println(aval+bval);
        
        
    }
}
 
发表于 2019-01-18 19:50:39 回复(0)
import java.util.Scanner;
public class Main{
    /*
    不用重复造轮子就是方便啊哈哈
    */
    public static void main(String[] args){
        try(Scanner in = new Scanner(System.in)){
            String s1 = in.next(),s2 = in.next();
            System.out.println(helper(s1,s2));
        }
    }
    public static int helper(String s1,String s2){
        String[] str1 = s1.split(","),str2 = s2.split(",");
        StringBuffer sb1 = new StringBuffer(),sb2 = new StringBuffer();
        for(String s:str1){
            sb1.append(s);
        }
        for(String s:str2){
            sb2.append(s);
        }
        int num1 = Integer.valueOf(sb1.toString()),num2 = Integer.valueOf(sb2.toString());
        return num1 + num2;
    }
}


发表于 2019-01-13 22:02:06 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String[] str2 = str.split(" ");
            StringBuilder sa = new StringBuilder();
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<str2[0].length();i++){
                if(str2[0].charAt(i) != ','){
                    sa.append(str2[0].charAt(i));
                }
            }
            for(int i=0;i<str2[1].length();i++){
                if(str2[1].charAt(i) != ','){
                    sb.append(str2[1].charAt(i));
                }
            }
            System.out.println(Integer.valueOf(sa.toString())+Integer.valueOf(sb.toString()));
        }
    }
}

发表于 2018-08-11 22:09:07 回复(0)

//无脑的

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String len=null;
        while((len=br.readLine())!=null){
            String[] str=len.replaceAll(",","").split(" ");
            long a=Long.parseLong(str[0]);
            long b=Long.parseLong(str[1]);
            System.out.println(a+b);
        }
        br.close();
    }
}
发表于 2018-04-13 14:54:26 回复(0)
/*
 * 分享一下,交流一下
 */
import java.util.Scanner;

public class Main{


public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while(in.hasNext()) {
        String a = in.next();
        String b = in.next();
        int A = Integer.parseInt(a.replaceAll(",", ""));
        int B = Integer.parseInt(b.replaceAll(",", ""));
        System.out.println(A + B);
    }
} 
}
编辑于 2018-04-05 13:33:53 回复(0)
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext())
{
String s;
String num1="",num2="";
s=scanner.nextLine();
String str[]=s.split(" ");
String s1[]=str[0].split(",");
for(int i=0;i<s1.length;i++)
num1 += s1[i];
String s2[]=str[1].split(",");
for(int i=0;i<s2.length;i++)
num2 += s2[i];
System.out.println(Integer.valueOf(num1)+Integer.valueOf(num2));
}
}

}

发表于 2017-09-07 23:18:28 回复(0)
import java.util.Scanner;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		String next1 = scanner.next();
		String next2 = scanner.next();

		String[] split1 = next1.split(",");
		String[] split2 = next2.split(",");

		StringBuilder stringBuilder1 = new StringBuilder();
		for (int i = 0; i < split1.length; i++) {
			stringBuilder1.append(split1[i]);
		}

		StringBuilder stringBuilder2 = new StringBuilder();
		for (int i = 0; i < split2.length; i++) {
			stringBuilder2.append(split2[i]);
		}

		System.out.println(Integer.valueOf(stringBuilder1.toString()) + Integer.valueOf(stringBuilder2.toString()));

	}
}


发表于 2017-06-18 18:24:41 回复(0)
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){
       String num1=sc.next();
       String num2=sc.next();
       String[] a1=num1.split(",");
       String[] a2=num2.split(",");
       int n1=0;
       int n2=0;
       if(!num1.startsWith("-"))
       {
      for(int i=a1.length-1;i>=0;i--)
           {
          n1+=Integer.parseInt(a1[i])*Math.pow(10, (a1.length-1-i)*3);
           }
      if(!num2.startsWith("-"))
      {
      for(int i=a2.length-1;i>=0;i--)
          {
          n2+=Integer.parseInt(a2[i])*Math.pow(10, (a2.length-1-i)*3);
          }
      }
      else
      {
      for(int i=a2.length-1;i>=0;i--)
          {
          n2+=Math.abs(Integer.parseInt(a2[i]))*Math.pow(10, (a2.length-1-i)*3);
          } 
      n2*=-1;
      }
       }
       else
       {
      for(int i=a1.length-1;i>=0;i--)
      {
      n1+=Math.abs(Integer.parseInt(a1[i]))*Math.pow(10, (a1.length-1-i)*3);
      } 
  n1*=-1;
  if(!num2.startsWith("-"))
      {
      for(int i=a2.length-1;i>=0;i--)
          {
          n2+=Integer.parseInt(a2[i])*Math.pow(10, (a2.length-1-i)*3);
          }
      }
      else
      {
      for(int i=a2.length-1;i>=0;i--)
          {
          n2+=Math.abs(Integer.parseInt(a2[i]))*Math.pow(10, (a2.length-1-i)*3);
          } 
      n2*=-1;
      }
  
       }
       
       
       System.out.println(n1+n2);
       
    }
   }

}

发表于 2017-05-30 16:22:07 回复(0)

import java.util.Scanner;

public class Main{

public static void main(String[] args){

   Scanner in=new Scanner(System.in);

    while(in.hasNext()){

   String a=in.next();

   String b=in.next();



     System.out.println(Long.valueOf(a.replaceAll(",", ""))+Long.valueOf(b.replaceAll(",", "")));

   }

   }

}

发表于 2017-05-08 15:56:19 回复(0)
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
        String a=scanner.next();
        String b=scanner.next();
        a=a.replaceAll(",", "");
        b=b.replaceAll(",", "");
        System.out.println(Integer.parseInt(a)+Integer.parseInt(b));
        }
}
}
发表于 2017-04-22 10:41:53 回复(0)
import java.util.Scanner;

public class Main {
	
	private static final int MAX=1000;
	public int add(String str1,String str2){
		str1=str1.replace(",", "");
		str2=str2.replace(",", "");
		int a=Integer.parseInt(str1);
		int b=Integer.parseInt(str2);
		return a+b;
	}
	public static void main(String[] args){
		Main main=new Main();
		String str1=new String();
		String str2=new String();
		int[] count=new int[MAX];
		int i=0;
		Scanner scanner=new Scanner(System.in);
		while(scanner.hasNext()){
			str1=scanner.next();
			str2=scanner.next();
			count[i]=main.add(str1, str2);
			i++;
		}
		for(int j=0;j<i;j++)
			System.out.println(count[j]);
		scanner.close();
	}
}


发表于 2017-04-04 22:56:48 回复(0)

这题最好通过valueOf()来做~~

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            fun(in.nextLine());
        }
        in.close();
    }
    public static void fun(String str){
        StringBuffer stra = null,strb = null;
        for(int i = 0; i < str.length(); i++){
            if(str.charAt(i) == ' '){
                stra = new StringBuffer(str.substring(0, i));
                strb = new StringBuffer(str.substring(i+1));
                break;
            }
        }
        for(int i = 0; i < stra.length(); i++)
            if(stra.charAt(i) == ',')
                stra.deleteCharAt(i);
        for(int i = 0; i < strb.length(); i++)
            if(strb.charAt(i) == ',')
                strb.deleteCharAt(i);

        System.out.println(Long.valueOf(stra.toString())+Long.valueOf(strb.toString()));
    }
}
发表于 2017-02-06 17:18:25 回复(0)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String input = scanner.nextLine();
String ANum = input.split(" ")[0];
String BNum = input.split(" ")[1];
int A = num(ANum);
int B = num(BNum);
System.out.println(A + B);

}
}

public static int num(String input) {
char[] data = input.toCharArray();
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < data.length; i++) {
if (data[i] != ',') {
buffer.append(data[i]);
}
}
return Integer.parseInt(buffer.toString());
}
}

发表于 2017-01-07 10:42:55 回复(0)