1001. A+B Format (20)
2018-01-19 14:49
204 查看
一、题目
二、个人理解
Tips:此题我一开始就踩了一个坑,就是按题目意思应该是从后往前每三位加逗号,而我却理解成从前往后了
此题采用字符串保存结果,最后反转输出
一些特殊情况,如0、负号、只有三位数字等
C++:
#include <iostream> #include <algorithm> using namespace std; int main() { int a,b,sum,temp,len=0,tag=1; string result=""; cin>>a>>b; sum=a+b; if(sum<0) { tag=-1;//此时表示负号 sum*=-1;//转为正 } if(sum==0) { result+='0';//sum为0情况 } while(sum) { temp=sum%10;//取最后一位 sum/=10; if(len%3==0&&len!=0) {//从后往前每三位加一个逗号 result+=','; } result+=('0'+temp); len++; } if(tag==-1) { result+='-';//加上负号 } reverse(result.begin(),result.end());//反转一下字符串 cout<<result; }
相关文章推荐
- 1001.A+B Format (20)的感受
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
- A1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT(甲级)1001. A+B Format (20)
- 1001. A+B Format (20)题解
- 浙大PAT (Advanced Level) Practise 1001 A+B Format (20)
- 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001 A+B Format (20)
- 【PAT甲级】【C】1001. A+B Format (20)
- PAT 1001 A+B Format (20)
- 1001.A+B Format(20)
- 1001. A+B Format (20)
- pat甲级1001. A+B Format (20)
- 1001. A+B Format (20)
- 【PAT】1001.A+B Format (20)
- 1001 A+B Format (20)
- 1001. A + B Format(20)