PAT 1001. A+B Format (20)
来源:互联网 发布:天猫和淘宝是一家吗 编辑:程序博客网 时间:2024/06/05 11:30
PAT甲级真题及训练集第一题:
计算A+B的值,并按照规定格式输出(从低位开始,每三位插入一个逗号)
代码如下:
#include<iostream>#include<cstdlib>using namespace std;int judge_low(int x);int main(){int a,b;cin>>a>>b;int c=a+b;if(c/1000==0) cout<<c;else if(c/1000000==0){int low=abs(c)%1000;int lnum=judge_low(low);int high=c/1000;cout<<high<<",";for(int i=0;i<lnum;i++){cout<<"0";} cout<<low;}else{int low=abs(c)%1000;int lnum=judge_low(low);int mid=(abs(c)/1000)%1000;int mnum=judge_low(mid);int high=c/1000000;cout<<high<<",";for(int i=0;i<mnum;i++){cout<<"0"; }cout<<mid<<",";for(int i=0;i<lnum;i++){cout<<"0"; }cout<<low;}return 0;} int judge_low(int x){//判断输出0的个数if(x<10){return 2;}else if(x<100){return 1;}else{return 0;}}
本方法需要注意0个数的输出
阅读全文
1 0
- PAT A 1001. A+B Format (20)
- PAT-A-1001. A+B Format (20)
- PAT A 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- Pat(A) 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- 【PAT】1001. A+B Format (20)
- PAT:1001. A+B Format (20)
- ( PAT )1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- [PAT]1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- 电源模块的PCB设计
- 全新安装Mac OSX 开发者环境 同时使用homebrew搭建 PHP,Nginx ,MySQL,Redis,Memcache ... ... (LNMP开发环境)
- MongoDB3.4对象存储(Journaling日志)
- Kafka学习笔记 --- Kafka消息机制
- Java中的四种引用介绍和使用场景
- PAT 1001. A+B Format (20)
- Bootstrap3 datetimepicker在AngularJs中使用实例
- POJ 3264 Balanced Lineup (RMQ)
- 网络电视机顶盒、IPTV后台内容管理系统?
- 在spring中获得HttpServletRequest 的三种方式
- js中的NUMBER类型
- Codeforces 689D RMQ+二分
- windows下make方式构建arducopter.px4
- 问题排查