PAT(Advanced Level) Practice
A1001 A+B Format
寒假在家学PAT Day1
A+B Format.
题意:计算A B两数之和,输出时使用标准格式输出:##,###,###
几个关键点:
1、正数与字符串类型的转换
#include
int m;
string str;
str=to_string(m);
2、int long long int 范围
int 4B
long long int 8B
代码如下(不是很简洁)
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b;
c = a + b;
if (c < 0)
{
cout << "-";
c = -c;
}
string str;
string m;
int temp = 0;
while (c > 999)
{
m = to_string(c % 1000);
if (c % 1000 < 10)
m = "00" + m;
else if (c % 1000 < 100)
m = "0" + m;
c = c / 1000;
if (temp == 0)
{
str =m+str;
temp = 1;
}
else
str = m + ","+str;
}
if (temp == 1)
str = to_string(c) + "," + str;
else
str = to_string(c);
cout << str;
return 0;
}
2020.2.6. 5 pm