2020.02.19【疫情时期!!!】
不为了写给任何人看,之前的编程能力太弱了,希望能慢慢的通过做PTA的题目重新拾起C++的语法知识和锻炼编程思维,慢慢为考研做准备,每一个题尽量做到一题不止一个解法。希望能坚持下去,希望有错误能得到指正
1001 A+B Format (20分)
Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
法1
题目的大意就是输入两个数,求和后按格式输出,要求是每三位需要加逗号隔开。第一反应感觉是一个字符串的题目,所以思路大致为先进行计算,然后转换为字符串,再通过特判条件加入逗号。
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string itos (long long num){
stringstream s;
s << num;
return s.str();
}
void dividenum(string numstr){
int length = numstr.length();
string ansstr;
//用来查看传入的数和长度观察规律
//cout << numstr << " " << length << endl;
for(int i = 0;i < length ;++i){
ansstr += numstr[i];
if(numstr[i] == '-'){
continue;
}
if((i + 1) % 3 == length % 3 && i != length - 1){
ansstr.append(",");
}
}
cout << ansstr << endl;
}
int main(){
long long a , b , ans;
cin >> a >> b;
ans = a + b;
//cout << ans << endl;
string str = itos(ans);
//cout << str;
dividenum(str);
return 0;
}
比较的保守的使用了long long,可能不是最佳的选择,需要根据题目条件进行调整
难点也就在这个特判条件上面,在进行字符串的每一位遍历的时可以发现只要当前位下标i满足(i + 1) % 3等于字符串长度 % 3并且i不是最后一位,就在逐位输出的时候在该位输出后的后面加上一个逗号,值得注意的是不能为长度的最后一位,不然的话必定满足 (i + 1) % 3 == length % 3的表达式,会在答案的末尾多一个逗号
主要使用了C++的stringstream 类实现字符串的转换,需要在源程序文件中包含头文件include< sstream >,同时使用append实现字符的追加,也可以使用+=
法2
第二个方法就比较易懂了,求和计算后,若为负数的话转换为正数,先把负号输出,再使用循环将每一位放入整型数组里面,然后进行特判添加逗号即可。值得注意的是,这种方法在计算结果为0时不存在负号也不会进入while循环,若不加对0的特判会有一组样例的错误
#include<iostream>
#include<string>
using namespace std;
void dividenum(long num){
int a[20] , count = 0;
if(num == 0){
cout << "0" << endl;
return;
}
if(num < 0){
cout << "-" ;
num = 0 - num;
}
while(num){
a[count] = num % 10;
count++;
num /= 10;
}
for(int i = count -1 ; i >= 0 ;i--){
cout << a[i];
if(i % 3 == 0 && i > 0){
cout << ",";
}
}
cout << endl;
}
int main(){
long long a , b , ans;
cin >> a >> b;
ans = a + b;
//cout << ans << endl;
dividenum(ans);
return 0;
}
法三
第三个方法是看了哔哩哔哩上的一名UP主的PAT题解学习来的,觉得非常的简便易懂,学习一下自己也试着做了一遍,将计算求和后的每一位使用push_back取出来放进字符串中,然后倒序输出,同时也学会了输出单个字符使用putchar()会比cout,printf()快
这里放上这个UP主的链接,题目讲解的很清晰,总结的刷题模板非常喜欢(对于我这种小白的帮助简直太大了),然后这个UP主有个特别棒的博客,上面有很多知识点的总结和博客,简直不要太喜欢哈哈哈
😊哔哩哔哩UP主-油炸麻花不带卷的PAT题解
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main(){
#ifdef ONLINE_JUDGE //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入
#else
freopen("1.txt", "r", stdin); //从1.txt输入数据
#endif
string ans;
long long a , b;
cin >> a >> b;
a += b;
int sum = a, counter = 0;
while(1){//使用循环逆向的放入199,999
//这里将整型变成字符'0' + 1 = '1',并加入字符串中
ans.push_back('0' + abs(sum % 10));
sum /= 10;
if(sum == 0){
break;
}
counter++;
if(counter % 3 == 0){
ans.push_back(',');
}
}
if(a < 0){//如果是负数加上负数
ans.push_back('-');
}
//最后再使用循环把字符串逆向输出
for(int i = ans.size() - 1 ;i >= 0 ; i--){
putchar(ans[i]);
}
return 0;
}