PAT甲级1001题解
来源:互联网 发布:淘宝金三塔官方旗舰店 编辑:程序博客网 时间:2024/05/16 17:41
题目要求如下:
1001. A+B Format (20)
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 9Sample Output
-999,991
注:这题是一道简单题,但是前几次提交都拿不到所有测试点,容易有所遗漏。比如1+1,输出002,还有-1+1未输出等低级错误。
(talk is cheap,show me the code.coding......)
#include<stdio.h>
#include<stdlib.h>
int number[10];
int main()
{
int a, b;
bool flag;//用于标记是否为负数
int sum;
while (scanf("%d%d", &a, &b) != EOF)
{
flag = 0;
sum = a + b;
if (sum < 0)
flag = 1;
if (flag == 1)
{
sum = -sum;
printf("-");
}
//int count = 0;
int i = 0;
if (sum == 0)
printf("%d", 0);
bool flag2 = 0;//用于标记是否小于1000
if (sum < 1000)
flag2 = 1;
while (sum)
{
number[i++] = sum % 1000;
sum /= 1000;
}
for (int j = i - 1; j >0; j--)
{
if (j != i - 1)
printf("%03d,", number[j]);
else
printf("%d,", number[j]);
}
if (i >= 1)
{
if (flag2 == 0)
printf("%03d", number[0]);
else
printf("%d", number[0]);
}
printf("\n");
}
return 0;
}
- PAT甲级1001题解
- PAT甲级题解目录
- PAT甲级1002题解
- PAT甲级题解目录
- PAT甲级1135----红黑树题解
- PAT甲级1134. Vertex Cover (25) 题解
- PAT甲级1001
- PAT甲级1001
- PAT甲级习题(1001)
- PAT-甲级-1001
- PAT甲级1001
- PAT甲级1001,1002
- PAT甲级1001
- PAT甲级1001
- 浙大PAT甲级-1001
- PAT甲级 1001
- PAT 甲级 1001
- PAT甲级1001-1004
- x86主机搭建家庭智能路由系统 ---- Proxmox虚拟化实现一机多用
- JavaScript检测类型
- POJ 2777 Count Color 线段树+小技巧
- iOS中的gzip解压
- 【天梯赛】L2-006. 树的遍历(层次遍历)
- PAT甲级1001题解
- Qt工程转VS工程
- java下载工具类,支持断点续传
- [DP] Tsin A1315 积木
- java多线程——ThreadLocal
- kubernetes单个pod运行两个容器yaml文件实践
- DG物理备库的几种模式
- GCD小结
- git 常用命令