PAT 1001 Format
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的更多相关文章
- PAT 1001 A+B 解题报告
PAT 1001 A+B 代码链接:传送门 题目链接:传送门 题目简述: 给定两个值a,b: 范围-1000000 <= a, b <= 1000000: 按指定格式输出a+b的结果,例: ...
- 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 ...
- pat 1001 A+B Format
题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...
- 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 ...
- PAT 1001. A+B Format(水题)
#include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...
- PAT 1001 A+B Format (20 point(s))
题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...
- PAT 1001 A+B Fotmat
源码 1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calcula ...
- PAT 1001
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...
- PAT 1001 害死人不偿命的(3n+1)猜想 (15)(C++&JAVA&Python)
1001 害死人不偿命的(3n+1)猜想 (15)(15 分) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反 ...
随机推荐
- 【AtCoder】AGC022 F - Leftmost Ball 计数DP
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
- Cloudera Manager Admin控制台启动不起来
这几天都在搞大数据这一块,由于以前自己在弄hadoop等安装的时候特别的费劲,于是乎找到了广大程序员的福音——cloudera manager,但是第一步安装好了以后无法启动,再三思考+百度发现: 通 ...
- 微服务深入浅出(3)-- 服务的注册和发现Eureka
现来说一些Eureka的概念: 1.服务注册 Register 就是Client向Server注册的时候提供自身元数据,比如IP和Port等信息. 2.服务续约 Renew Client默认每隔30s ...
- 汇编与C语句
---恢复内容开始--- 汇编与C语句 4.1C语句与汇编 学习了汇编语言之后,就需要将常用的C语言代码结构与相应的汇编语言联系起来.这样就可以在分析汇编语言的时候,明白它的意思.C语言中函数过程的调 ...
- 【bzoj题解】题解传送门
如题,题解传送门: 1001 1008 1012
- 渗透测试===kali linux的安装
方法一: kali linux 安装在本地的vitural box 或者 wm ware中 方法二: 安装在移动硬盘或者储存卡中,插到电脑就能用
- 虚拟机使用主机ss代理
环境Linux mint 设置好主机ss代理,并开启[允许来自局域网的链接] 在Linux虚拟机的system setting-network手动设置代理 地址全部填入刚刚的主机地址,端口号为主机ss ...
- 使用数据库管理工具打开MySql
1.推荐使用软件:Navicat_Premium_11.0.10.exe. 下载地址:http://pan.baidu.com/s/1nu6mTF7 2.下载上面文件并安装. 3.打开Navicat ...
- Codeforces Round #504 E. Down or Right
Codeforces Round #504 E. Down or Right 题目描述:交互题. 有一个\(n \times n\)的方阵,有一些格子是障碍,从\((1, 1)\)出发,只能向右向下走 ...
- vue+elementui 新增和编辑如何实现共用一个弹框
//html代码: //按钮 <el-button type="primary" size="medium" @click="addEquipm ...