Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
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.
-1000000 9
-999,991
import java.util.Scanner; //没什么难点,从第一位开始输出字符。 //count计数,每输出了三个字符(且不为最后一个字符)就输出一个“,” public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); long a=in.nextLong(); long b=in.nextLong(); long c=a+b; if(c<0){ System.out.print("-"); c=0-c; } String s=String.valueOf(c); String out=""; int count=0; for(int i=s.length()-1;i>=0;i--){ out=s.charAt(i)+out; count++; if(count%3==0&&i!=0){ count=0; out=","+out; } }System.out.println(out); } }
package go.jacob.day822; import java.util.Scanner; /** * 1001. A+B Format (20) * * @author Jacob * 思路:从后往前遍历,每个三位加","。同时要考虑前一位是否为"-"和总位数是否小于4 */ public class Demo2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(), b = sc.nextInt(); String sum = a + b + ""; int index = sum.length() - 3; StringBuilder res = new StringBuilder(); while (index > 0) { if (sum.charAt(index - 1) != '-') { res.insert(0, "," + sum.substring(index, index + 3)); } else break; index -= 3; } res.insert(0, sum.substring(0, index + 3)); System.out.println(res); sc.close(); } }
// 差点忘了判断边界,
//而且第一次代码错了,竟然也过了(/ w\)
//蓝后用栈再做了一下~~^_-
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <stdlib.h>
#include <stack>
#include <deque>
#include <cstring>
using namespace std;
#define Mod 1000000007
void slove(int x);
int main()
{
int a, b, ans;
cin>>a>>b;
ans=a+b;
slove(ans);
return 0;
}
void slove(int x){
if(x==0) {cout<<"0"<<endl;return ;}
if(x<0){cout<<"-"; x=-x;}
stack<int>s;
while(x){
int t=x%10;
s.push(t);
x/=10;
}
while(!s.empty()){
cout<<s.top();
s.pop();
int len=s.size();
if(len%3==0&&len) cout<<",";
}
cout<<endl;
}
#include<stdio.h> int main() { int x, y; while (~scanf("%d%d", &x, &y)) { int sum = x + y; if (sum < 0) { printf("-"); sum = -sum; } if (sum<1000 && sum>-1000) printf("%d\n", sum); else if (sum<1000000 && sum>-1000000) printf("%d,%03d\n", sum / 1000, sum % 1000); else printf("%d,%03d,%03d\n", sum / 1000000, sum / 1000 - 1000 * (sum / 1000000), sum % 1000); } return 0; }
#include<iostream> using namespace std; int main(){ int a,b,sum; int num[10]; //倒序存储和的绝对值的数组 cin>>a>>b; sum=a+b; if(sum<0){ cout<<"-"; sum=-sum; } int i; for(i=0;sum>0;i++){ num[i]=sum%10; sum/=10; } for(int j=i-1;j>=0;j--){ cout<<num[j]; if(j%3==0&&j!=0) cout<<","; } return 0; }
#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int a,b,c; cin >> a >> b; c=a+b; if(c>=-999 && c<=999) cout << c; else{ if(c<0) cout << "-"; c=max(c,-c); vector<char> ans; int flag=0; while(c){ int tmp=c%10; if(flag==3){ flag=0; ans.push_back(','); } ans.push_back(tmp+'0'); flag++; c=c/10; } for(int i=ans.size()-1;i>=0;i--) cout << ans[i]; } return 0; }
#include<iostream> #include<vector> #include<string.h> #include<map> #include <string> using namespace std; void aplusbformat() { long long a, b; vector<long long>q; cin >> a >> b; a += b; if (a < 0) { cout << "-"; a = -a; } else if (a == 0) { cout << "0"; } while (a != 0) { q.push_back(a%10);// a /= 10; } vector<string> qp; auto it = q.begin(); int i1 = 0; while (it != q.end()) { if (i1 != 0&&i1 % 3 == 0) { qp.push_back(","); } i1++; string s = to_string(*it); qp.push_back(s); it++; } while (qp.size() != 0) { cout << qp.back(); qp.erase(qp.end()-1); } } //-1000000 9 int main() { aplusbformat(); return 0; }
#include <iostream>
#include <string>
using namespace std;
int main() {
//第一种方法
int a, b, c;
string s;
cin>>a>>b;
c = a + b;
s = to_string(c);// to_string 将数值转化为字符串。返回对应的字符串。
int n = s.size();
int temp = 0;
if (c < 0) temp = 1;
for (int i = n - 3; i > temp; i -= 3) {
s.insert(i, ",");// s.insert(pos, str),pos是待插入的位置, str是待插入的字符串
}
cout << s << endl;
//第二种方法
int k[8],i=0,flag=0;//建立数组,两个七位数相加最多八位数,所以选择k[8]
if(c<0){
c=-c;
flag=1;
}
if(c==0)cout<<"0";
else{
while(c)
{
k[7-i]=c%10;
c=c/10;
i++;
}
if(flag)cout<<"-";
for(i=8-i;i<8;i++)//8-i为该数组的起始位置
{
cout<<k[i];
if(i==1||i==4)
cout<<",";
}
}
return 0;
}
import java.util.Scanner;public class Main{static Scanner scanner = new Scanner(System.in);public static void main(String[] args) {int a = scanner.nextInt();int b = scanner.nextInt();String c = String.valueOf(a + b);StringBuilder s = new StringBuilder();int flag = 0;for (int i = c.length() - 1; i >= 0 ; i--) {s.append(c.charAt(i));flag++;if (flag % 3 == 0){s.append(',');}}String x = s.reverse().toString();x = x.replace("-,","-");if (x.startsWith(",")){x = x.substring(1);}System.out.println(x);}}
python两行搞定
a,b = map(int,input().split()) c = str(a + b) lsc= [] for i in c: lsc.append(i) cnt = 0 lsout = [] for i in range(len(lsc)-1,-1,-1): if lsc[i]>='0' and lsc[i]<='9': cnt+=1 if cnt>6: lsout = lsc[:len(lsc)-6]+[","]+lsc[len(lsc)-6:len(lsc)-3]+[',']+lsc[len(lsc)-3:] elif cnt>3: lsout = lsc[:len(lsc)-3]+[',']+lsc[len(lsc)-3:] out = '' for i in lsout: out = out + i if out!='': print(out) else: for i in lsc: out = out + i print(out)
#include <iostream> #include <vector> using namespace std; vector<int> divided_sum; int main() { int a, b; cin >> a >> b; int sum = a + b; if (sum < 0) { cout << "-"; sum = -sum; } if (sum == 0) { cout << "0"; } else { int temp = 0; while (sum != 0) { temp = sum % 1000; divided_sum.push_back(temp); sum = sum / 1000; } for (int i = divided_sum.size() - 1; i >= 1; i--) { cout << divided_sum[i] << ","; } cout << divided_sum[0]; } system("pause"); return 0; }
#include <iostream> #include <vector> #include <string> #include <sstream> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; // 思路: 题目的意思就是A+B 然后输出C,C有一定的格式,每大于3个数要输出一个“,” // 个人的思路就是先给一个count 赋初值。如果这个初值为1 就是把纯数字的部分凑层3的倍数这样才能从大到小的添加“,” // That's all,Thinks! int main(int argc, char** argv) { long A; long B; cin >> A >> B; long C = A + B; stringstream ss; ss << C; string C2; ss >> C2; int count = 0; int i = 0; if(C<0){ cout<< "-"; i = 1; } count = 3 - (C2.size()-i) % 3; if(count == 3) count = 0; for(; i<C2.size(); i++) { count ++; cout << C2[i]; if(count >= 3 && i != C2.size() - 1) { cout << "," ; count = 0; } } cout << endl; //while(1); return 0; }
请注意了,由于测试用例有一些不同,在牛客网提交时不判断a+b的结果为0的情况时也可以通过测试用例,但在PAT中提交必须判断a+b等于0的情况,否则会有一个测试点不通过。
#include <iostream> #include <string> using namespace std; int main() { long A,B,sum; bool flag=false; cin>>A>>B; char num[]={'0','1','2','3','4','5','6','7','8','9'}; string str=""; int i=0; sum=A+B; if(sum<0) { sum=-sum; flag = true; } while(sum) { i++; str = num[sum%10] + str; sum /=10; if((i%3)==0 && sum!=0) str ="," + str; } if(flag) str ="-" + str; cout<<str; return 0; }