题目:

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

题意:

给你两个数字a,b,其中a,b的范围是 −10​^6​​≤a,b≤10​^6​​. 让你求出来a+b的结果,注意每三位要用一个“,”分隔。

思路:

把a+b的结果算出来,然后把结果拆分开,每三位加入一个分隔符“,”;最后注意正负号的输出和分隔符在首尾的位置不需要添加;

代码如下:

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<iostream>
using namespace std;int a,b;
string s;int main()
{while(~scanf("%d%d",&a,&b)){int sum=a+b;//计算出来a+b的结果;if(sum==0)//注意,否则第四组测试数据会错;printf("0\n");else{s="";//清空字符串;int fu=0;//符号;if(sum<0)fu=1;//注意负号的输出;sum=fabs(sum);int k=0;while(sum!=0)//把数字拆分开存在字符串中,注意每三位加入一个“,”;{char tt=sum%10+'0';sum/=10;s=s+tt;k++;if(k==3){s=s+",";//加入“,”;k=0;}}if(fu==1)//符号输出;cout<<"-";int l=s.size()-1;//注意下标是从0开始的;if(s[l]!=',')//注意第一位不用添加分隔符;cout<<s[l];for(int i=l-1; i>=0; i--){cout<<s[i];}cout<<endl;}}return 0;
}

(甲)1001 A+B Format (20 分)相关推荐

  1. 【PAT甲】1001 A+B Format (20分) 格式化输出

    problem 1001 A+B Format (20分) Calculate a+b and output the sum in standard format – that is, the dig ...

  2. 1001 A+B Format (20分)——12行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Calculate a+b and output the sum in standard format – that is, th ...

  3. 【PAT (Advanced Level) Practice】1001 A+B Format (20 分)

    #include<iostream> #include<string> #include<cstring> #include<cstdio> #incl ...

  4. 1001 A+B Format (20 分)【难度: 简单 / 知识点: 模拟】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 方法一: #include<bits/ ...

  5. PTA 甲级 1001 A+B Format (20分)

    这题我不会,是抄的刘婼大神的.地址如下 地址 #include <iostream> #include<string> using namespace std; int mai ...

  6. PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】

    题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...

  7. PAT甲题题解-1001. A+B Format (20)-字符串处理,水

    计算A+B的和,并且按标准格式处理,每3个就要有个逗号 #include <iostream> #include <cstdio> #include <algorithm ...

  8. 1001. A+B Format (20)---------------PAT开始

    今天是第一次用Java提交PAT的代码: 源代码不能直接提交,需要经过修改: 源代码: 1 package test;//提交时不能显示这句 2 3 import java.util.Scanner; ...

  9. 1001.A+B Format (20) 解题

    代码入口(https://github.com/NSDie/object-oriented) 这题的解题思路我有两个: 第一个是两个数字相加然后判断位数,因为题目限制了范围1000000的绝对值以内嘛 ...

  10. PAT甲级1001.A+B Format(20)

    ---恢复内容开始--- 题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 对于该题 ...

最新文章

  1. 【建模必备】遗传算法的基本原理与步骤(变异)
  2. php myadmin utf,phpmyadmin在哪里设置utf_8
  3. java中的构造方法与代码块
  4. 桥接模式 - 设计模式学习
  5. 大数据模型研究报告pdf_业绩大数据分析报告模型
  6. Tensorflow基于mnist数据集实现AlexNet
  7. com app.html,downloadApp.html
  8. Proteus的使用总结
  9. matlab绘正态/卡方/t/F分布概率密度图
  10. 数据表底层的B+树的叶子结点为啥用类似双链表连接起来
  11. 我有一个梦想计算机工程师作文,我有一个梦想作文(通用10篇)
  12. 计算机桌面无法解锁,电脑win10系统锁屏后解锁却无法进入桌面的解决方法
  13. gradle7打包libs目录中的jar生成fat jar
  14. 五款最出色的数据恢复工具
  15. 解决WIN 7 不能任意修改无线网卡MAC地址的问题
  16. taro: 如何使用编译配置中定义的常量defineConstants
  17. zabbix 监控域名注册到期时间
  18. 机械工程基础知识点汇总
  19. 【Go入门】Socks5代理服务器实现 翻译api调用 | 青训营笔记
  20. c语言山东科技大学答案oj,山东科技大学OJ答案.doc

热门文章

  1. python破解wifi易懂源代码
  2. PS怎么做彩色人物粉末烟雾爆炸特效果
  3. 第十二章:email-mailbox:管理email归档-imaplib:IMAP4客户库-获取消息
  4. Gamium创世纪建筑物的奇妙之处
  5. 电脑运行android 吃鸡,安卓游戏粉福音!凤凰系统最新版本发布:电脑玩吃鸡游戏更刺激?...
  6. 【问题解决】python中round()函数并非四舍五入
  7. 国人孜孜不倦的探索传统文化中汉字的博大精深,解密《汉字工程密码》
  8. 暗幽***风恋组综合工具解压安装包2013元月27日更新版
  9. 最全控件,开发工具,国外软件
  10. 关于讯飞语音听写RecognizerDialog 去除这个弹框view中的任何控件 更改其中内容