A+B Format
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
这道题就是简单的加法运算,注意格式输出就好
C++实现
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b;
c = a+b;
if(c < 0) cout << "-";
c = abs(c);
string s = to_string(c);
int t = s.length()%3, t1=0;
for(int i = 0; i < t; i++) cout << s[i];
if(t && s.length()>3) cout << ",";
for(int i = t; i < s.length(); i++){
if(t1%3==0 && t1!=0) cout << ",";
cout << s[i];
t1++;
}
return 0;
}
Python实现
if __name__ == "__main__":
line = input().split(" ")
a, b = int(line[0]), int(line[1])
print('{:,}'.format(a+b))
Java实现
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split(" ");
br.close();
int temp = Integer.valueOf(line[0]) + Integer.valueOf(line[1]);
if(temp < 0){
System.out.print("-");
}
String s = String.valueOf(Math.abs(temp));
String a = new StringBuffer(s).reverse().toString();
String get = "";
for(int i=0;i<a.length();i++){
get = get + a.charAt(i);
if((i+1) %3 == 0 && i != a.length() - 1)
get = get + ',';
}
String out = new StringBuffer(get).reverse().toString();
System.out.print(out);
}
}
本文详细介绍了如何使用C++、Python和Java解决A+B问题,并强调了输出格式的重要性,确保数字以标准格式显示,每三位数字用逗号分隔。
4690

被折叠的 条评论
为什么被折叠?



