problem

1001 A+B Format (20)(20 point(s))
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 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. Output 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

anwser

#include <iostream>
int main(){
int a, b;
int c;
int end[10];
std::cin>>a>>b;
c = a+b;
if (c==0) {
std::cout<<"0";
return 0;
}
if (c<0) {
std::cout<<"-";
c = -c;
}
int index = 0;
while(c>0){
end[index++] = c%1000;
c/=1000;
}
for(int i = index - 1; i >= 0; i--){
if(i != index-1){
if(end[i] == 0)
std::cout<<"000";
if(end[i] > 99)
std::cout<<end[i];
else if(end[i] > 9){
std::cout<<"0";
std::cout<<end[i];
}
else if (end[i] > 0){
std::cout<<"00";
std::cout<<end[i];
}
}
else
std::cout<<end[i];
if (i != 0)
std::cout<<",";
}
return 0;
}

experience

  • 边界条件要注意的

    • c ==0
  • 除首个数字外 end[i] == 0 的情况,并且要分类讨论
  • 首个数字不需要特殊处理

PAT 1001 Format的更多相关文章

  1. PAT 1001 A+B 解题报告

    PAT 1001 A+B 代码链接:传送门 题目链接:传送门 题目简述: 给定两个值a,b: 范围-1000000 <= a, b <= 1000000: 按指定格式输出a+b的结果,例: ...

  2. PAT 1001. A+B Format 解题

    GitHub PDF 1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, t ...

  3. pat 1001 A+B Format

    题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...

  4. PAT 1001 A+B Format (20分) to_string()

    题目 Calculate a+b and output the sum in standard format -- that is, the digits must be separated into ...

  5. PAT 1001. A+B Format(水题)

    #include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...

  6. PAT 1001 A+B Format (20 point(s))

    题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...

  7. PAT 1001 A+B Fotmat

    源码 1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calcula ...

  8. PAT 1001

    Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...

  9. PAT 1001 害死人不偿命的(3n+1)猜想 (15)(C++&JAVA&Python)

    1001 害死人不偿命的(3n+1)猜想 (15)(15 分) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反 ...

随机推荐

  1. 37、使用FileInputStream和FileOutputStream读取和写入

    绝对路径和相对路径 绝对路径:指文件在硬盘上真正存在的路径,比如:D:\Java\HelloWorld.java,这个指的是在电脑的d盘下的Java文件夹里面有个HelloWorld.java文件 相 ...

  2. F. Make It Connected(krustra+)

    题目链接:http://codeforces.com/contest/1095/problem/F 题目大意:首先给你n个点,然后给你每个点的权值,再给你m条边,这些边可以选也可以不选,然后问你要使这 ...

  3. WeX5入门之HelloWorld

    学习目标:数据双向绑定 在ui2上右键 新建一个应用 然后会出现一个目录 右键hello 在创建页面 选择标准的空白模板 并起一个名 自动生成这两个文件 建立一个input组件 再建一个output组 ...

  4. Lucene7.1.0版本的索引创建与查询以及维护,包括新版本的一些新特性探索!

    一 吐槽 lucene版本更新实在太快了,往往旧版本都还没学会,新的就出来,而且每个版本改动都特别大,尤其是4.7,6,6,7.1.......ε=(´ο`*)))唉,但不可否认,新版本确实要比旧版本 ...

  5. jumpserver安装教程

    centos7系统一步一步安装jumpserver 参照官方文档,查找了百度所有的文档,基本上都是按照官方的文档操作的 官方文档点我-> 安装jumpserver需注意: 1:网络环境要好,有的 ...

  6. jenkins打包安卓项目

    jenkins打包安卓项目和其它项目差不了太多. 1.构建选择 gradle(如果不用gradle自己写脚本编译也可) 2.jenkins用户需要安装JDK.SDK,jenkins会自动下载gradl ...

  7. session的本质及如何实现共享?

    为什么有session? 首先大家知道,http协议是无状态的,即你连续访问某个网页100次和访问1次对服务器来说是没有区别对待的,因为它记不住你. 那么,在一些场合,确实需要服务器记住当前用户怎么办 ...

  8. Python 离线环境

    一.应用场景 比如:对于数据安全要求比较严格的机房,服务器是不允许上网的.那么我现在开发了一套python程序,需要一些模块,怎么运行? 二.离线包制作 有2个解决方案: 1. 使用requireme ...

  9. 20165333实验一 JAVA开发环境的熟悉

    JAVA开发环境的熟悉-1 1建立"自己学号exp1"的目录 2 在"自己学号exp1"目录下建立src,bin等目录 3 javac,java的执行在&quo ...

  10. 适合新手的web开发环境

    学习web开发,环境搭建是必不可少的一个环节.你可以使用wamp一键安装包,或者使用sae.bae.gae这种PaaS平台来部署,或者安装*nix系统在本地部署. 对于一个希望体验LAMP式建站的新手 ...