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)砍掉一半.这样一直反 ...
随机推荐
- 【转】.NET+AE开发中常见几种非托管对象的释放
尝试读取或写入受保护的内存.这通常指示其他内存已损坏. 今天在开发时遇到一个问题:" 未处理 System.AccessViolationException Message="尝试 ...
- Python练习-基于socket的FTPServer
# 编辑者:闫龙 import socket,json,struct class MySocket: with open("FtpServiceConfig","r&qu ...
- 彻底搞懂字符编码(unicode,mbcs,utf-8,utf-16,utf-32,big endian,little endian...)[转]
最近有一些朋友常问我一些乱码的问题,和他们交流过程中,发现这个编码的相关知识还真是杂乱不堪,不少人对一些知识理解似乎也有些偏差,网上百度, google的内容,也有不少以讹传讹,根本就是错误的(例如说 ...
- Hyperledger Fabric1.0.0搭建
系统环境: 阿里云新装的Centos7.2 yum -y update yum install -y openssl openssl-devel gcc gcc-c++ zlib zlib-devel ...
- 电脑蓝屏提示unexpected store exception的解决方法
在我们使用电脑的过程中常常会遇到许多问题,对于许多不熟悉电脑的用户常常摸不着头脑,而部分用户在使用电脑时,电脑常常出现蓝屏的情况,并提示你的电脑遇到问题需要重新启动,与此同时,其报告错误代码为“une ...
- C# byte[] 转换16进制字符串
1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...
- USACO 6.4 Electric Fences
Electric FencesKolstad & Schrijvers Farmer John has decided to construct electric fences. He has ...
- Python全栈开发之目录
基础篇 Python全栈开发之1.输入输出与流程控制 Python全栈开发之2.运算符与基本数据结构 Python全栈开发之3.数据类型set补充.深浅拷贝与函数 Python全栈开发之4.内置函数. ...
- php实现微信分享朋友圈
class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appSecret) ...
- 一台Windows下配置多个Tomcat服务器
上一篇博客<Windows下配置Tomcat服务器>讲了,如何在一台Windows机器上配置一个Tomcat服务器.这篇介绍一下如何在一台Windows机器上配置多个Tomcat. 第一步 ...