本文共 2002 字,大约阅读时间需要 6 分钟。
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).
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.
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
C++ 代码如下:
#include#include #include #include #include #include using namespace std;stack re;int a,b,c;int flag=0;int main(){ scanf("%d%d",&a,&b); int c=a+b; if(c==0) { printf("0\n"); return 0; } if(c>0) flag=1; while (c) { re.push(c%1000); c/=1000; } if(!flag) printf("-"); int Size=re.size(); for (int i=0;i
Java 代码如下:
public class Adv1001 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a,b,sum,flag=0; a = sc.nextInt(); b = sc.nextInt(); sum = a+b; if(sum<0) flag=1; sum = Math.abs(sum); if(sum==0) { System.out.println(sum); return ; } Listlist = new ArrayList<>(); if(flag==1) System.out.print("-"); while(sum!=0) { list.add(sum%1000); sum/=1000; } if(list.size()==1) { System.out.println(list.get(0)); return ; } for (int i=list.size()-1;i>=0;i--) { if(i!=list.size()-1) { if(list.get(i)>=100) System.out.print(list.get(i)); else if(list.get(i)>=10) System.out.print("0"+list.get(i)); else System.out.print("00"+list.get(i)); } else System.out.print(list.get(i)); if(i!=0) System.out.print(","); } System.out.println(); }}
转载地址:http://kxaen.baihongyu.com/