博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT (Advanced Level) Practice 1001 A+B Format (20 分)
阅读量:3904 次
发布时间:2019-05-23

本文共 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).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −10​6​​≤a,b≤10​6​​. 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 
#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 ;        }        List
list = 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/

你可能感兴趣的文章
vb 中如何做同步 异步?
查看>>
geturl
查看>>
关于sizeof
查看>>
windows 核心编程笔记.070301
查看>>
WINDOWS核心编程笔记 070303
查看>>
终于解决了交叉表左上角,每页都显示的问题.
查看>>
windows核心编程 070309
查看>>
哈,又解决水晶报表的一个难题
查看>>
VC Ini文件处理
查看>>
一直误解sql事务的用法.
查看>>
转:利用C#实现分布式数据库查询
查看>>
转:Remoting系列(三)----对象的生命周期管理
查看>>
转:Remoting系列(二)----建立第一个入门程序
查看>>
转:Remoting系列(一)----Remoting的基本概念
查看>>
转:NET Remoting程序开发入门篇
查看>>
Net Remoting Singleton and Singlecall 区别
查看>>
2016年安大校赛(补题)
查看>>
BESTCODER ROUND92 1001.Skip the Class
查看>>
POJ 1661 Help Jimmy
查看>>
百练OJ 2755 神奇的口袋(递归+递推)
查看>>