PAT (Advanced) 1001. A+B Format (20)
来源:互联网 发布:双色球杀红球算法 编辑:程序博客网 时间:2024/06/13 11:54
原题:1001. A+B Format (20)
解题思路:
1.将数字直接转换成反向的字符串,在适当位置加入逗号即可;
2.字符串从0开始计数,逗号位置分别为 3, 7,11 , 15…… 可发现对每个逗号出现的位置 i ,有 i % 3 在 0, 1, 2 三个数间循环,以此添加逗号即可
3.0 要特殊处理;
C++代码如下:
#include<cstdio>#include<algorithm>using namespace std;char num[20];int trans(int a){ int i = 0; int x = 0; if(a == 0) num[i++] = '0'; while(a > 0) { if(i % 4 == x && i != 0) { x++; x = x % 3; num[i++] = ','; } num[i++] = a % 10 + '0'; a = a / 10; } return i;}int main(){ int m, n; while(scanf("%d%d", &m, &n) != EOF) { int sum = m + n; int len = trans(abs(sum)); if(sum < 0) printf("-"); for(int i = len - 1; i >= 0; i--) { printf("%c", num[i]); } printf("\n"); } return 0;}
阅读全文
0 0
- PAT (Advanced) 1001. A+B Format (20)
- PAT(Advanced level) 1001. A+B Format
- 【PAT Advanced Level】1001. A+B Format (20)
- 【c++】PAT (Advanced Level) 1001. A+B Format (20)
- 【PAT (Advanced Level)】1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) 1001. A+B Format (20) 解题报告
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT Advanced 1001. A+B Format (20) (C语言实现)
- 【PAT】【Advanced Level】1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
- PAT (Advanced Level) Practise 1001. A+B Format
- 浙大 PAT Advanced level 1001. A+B Format
- 浙大PAT (Advanced Level) Practise 1001 A+B Format (20)
- SSH与SSM学习之hibernate16——表与表的三种关系
- JSP_Server.xml配置虚拟路径
- 基于JAVA Swing,Hibernate,MySQL模拟QQ
- HTML学习之路02--img标签
- Android 一起来看看 7.0 的新特性 FileProvider
- PAT (Advanced) 1001. A+B Format (20)
- 灌水的一天
- windows核心编程之亲缘性
- 编译原理与编译构造 文法的优化1
- python Requests 初级
- Android6.0+运行时权限的处理
- 所谓的跨域(Cross-Origin),究竟是什么?——你所不知道浏览器跨域的小知识
- TCP,UDP,HTPP关系
- android Thread和Runable区别 精讲