1001. A+B Format (20)
2017-01-09 00:52
295 查看
题意:两个数求和,对于和从后往前每3个数输出一个",",直到剩余的数小于4.
解题思路:用字符串模拟整数进行操作。
用到C++ string中的insert函数在指定位置插入一个字符 insert有2个参数:第一个为迭代器的位置 第二个为要在该位置插入的字符。
和分情况讨论:
1. 为0 直接输出0 返回
2。小于0 输出"-" 号 取相反数后 进行有关操作。
先试了下insert函数如下:
本题代码:
解题思路:用字符串模拟整数进行操作。
用到C++ string中的insert函数在指定位置插入一个字符 insert有2个参数:第一个为迭代器的位置 第二个为要在该位置插入的字符。
和分情况讨论:
1. 为0 直接输出0 返回
2。小于0 输出"-" 号 取相反数后 进行有关操作。
先试了下insert函数如下:
string s = "lpho"; string::iterator iter; iter = s.begin(); //iter++; //iter++; iter += 2; s.insert(iter, 'w'); cout << s.c_str() << endl; //输出lpwho..
本题代码:
#include "iostream" #include "cstring" using namespace std; int main() { int a, b; int sum; string str_ans; cin >> a >> b; sum = a + b; if (sum == 0) { cout << 0 << endl; return 0; } if (sum < 0) { cout << "-"; sum = -sum; } int k = 0; while (sum != 0) { int temp = sum % 10; if(k>0 && k %3==0 ) str_ans.insert(str_ans.begin(),','); str_ans.insert(str_ans.begin(), temp+'0'); sum /= 10; k++; } cout << str_ans.c_str() << endl; return 0; }
相关文章推荐
- 1001. A+B Format (20)
- 【PAT Advanced Level】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)(AB和的标准输出)
- 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20) C++
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)——C语言
- 1001.A+B Format (20)
- PAT - 甲级 - 1001. A+B Format (20)(字符串处理)
- 1001. A+B Format (20)
- 1001. A+B Format (20)