博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级 1001
阅读量:4333 次
发布时间:2019-06-07

本文共 2012 字,大约阅读时间需要 6 分钟。

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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where 106​​a,b106​​. 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 两种思路,第一种是今天写的,将结果预处理好存在字符串里直接输出
1 #include
2 #include
3 #include
4 using namespace std; 5 int main() 6 { 7 long long a,b,c; 8 char s[100]; 9 while(scanf("%lld%lld",&a,&b)!=EOF)10 {11 c=a+b;12 int flag=0;13 if(c<0)14 {15 flag=1;16 c=0-c;17 }18 int l=0;19 int g,w=0;20 while(c)21 {22 g=c%10;23 c/=10;24 s[l++]=g+'0';25 w++;26 if(w%3==0)s[l++]=',';27 }28 if(a+b==0)cout << "0" <
=0;i--)34 cout << s[i];35 cout <

 

另一种是去年写的,一边输出一边加逗号,翻看以前的代码被自己秀到了。。。
1 #include
2 #include
3 #include
4 using namespace std; 5 int main() 6 { 7 int a,b,c,d=1; 8 while(scanf("%d%d",&a,&b)!=EOF) 9 {10 a+=b;11 c=0;12 d=1;13 if(a<0){printf("-"); a=0-a;}14 b=a;15 while(a/=10)16 {17 c++;18 d*=10;19 }20 c++;21 for(;c>0;)22 {23 printf("%d",b/d);24 b%=d;25 d/=10;26 c--;27 if(!(c%3)&&c)printf(",");28 }29 printf("\n");30 }31 return 0;32 }

 

转载于:https://www.cnblogs.com/LowBee/p/10439147.html

你可能感兴趣的文章
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>