Keywords: less

# 1001 A+B Format

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 −10
​6
​​ ≤a,b≤10
​6
​​ . 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
The first time I wrote 18 points, one of them was my negligence. This question is mainly to record my understanding of to [string, = = I didn't have any understanding. I found it later when I searched for the error. It felt very useful. When I write, I think it's better to turn it into a string, and then I can simulate it again (there's an oversight during the simulation). My simulation is different from most of the online ones. This simulation idea is accumulated by doing problems these days, but it's nothing

Here's a 20 point code
1. Sscanf and ssprintf are all contacted before character conversion, so the first thing I think about when I do this is this. It's very comfortable to master this. For example, the only trouble I think is that the parameter must be char array,

```#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
const int N = 205;
const int INF = 1000000000;
using namespace std;
int main(){
int a,b,c;
cin>>a>>b;
c = a + b;
if(c<0){
c = -c;
cout<<'-';
}
if(c < 1000) cout<<c;
else{
char s[9];
sprintf(s,"%d",c);
int flag = 0;
string ans;
for(int i=strlen(s)-1;i>=0;i-=3){
if(i !=strlen(s) - 1) ans+=',';
for(int j=0;j<3;j++){
if(i-j>=0) ans+=s[i-j];
else{
flag = 1;
break;
}
}
if(flag == 1) break;
}
reverse(ans.begin(),ans.end());
cout<<ans;
}

return 0;
}
```

The second type is to_string, the converted String type. In fact, when comparing later, I want to use stringstream. First, I don't know. Second, I haven't reached that point. At that time, I have to use this method to master it. I believe there will be some later, and I don't know whether to_string can be used in the examination room. It's very convenient. The idea is the same as my first one

```#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
const int maxn = 205;
const int INF = 0x3f3f3f3f;
using namespace std;
int main(){
int a,b,c;
cin>>a>>b;
c = a+b;
if(c<0) {
c = -c;
cout<<'-';
}
if(c<1000) cout<<c;
else{
string ans = to_string(c);
string s;
int flag = 0;
for(int i=ans.length() -1;i>=0;i-=3){
if(i!=ans.length()-1) s+=',';
for(int j=0;j<3;j++){
if(i-j>=0) s+=ans[i-j];
else {
flag = 1;
break;
}
}
if(flag ==1) break;
}
reverse(s.begin(),s.end());
cout<<s;
}

return 0;
}
```

This question simply simulates and grasps to string. Later, I also looked at the ideas of other gods, and I was too lazy to record them. This kind of question is based on my first feeling

# 1002 A+B for Polynomials

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N
​1
​​ a
​N
​1
​​
​​ N
​2
​​ a
​N
​2
​​
​​ ... N
​K
​​ a
​N
​K
​​
​​

where K is the number of nonzero terms in the polynomial, N
​i
​​ and a
​N
​i
​​
​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10，0≤N
​K
​​ <⋯<N
​2
​​ <N
​1
​​ ≤1000.

Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:
3 2 1.5 1 2.9 0 3.2
For the first time, 17 points. How to say, the problem is not comprehensive. If the coefficient is zero when adding polynomials, it should be removed at this time,
I use map,
In fact, this problem is not made independently, because I forgot how to write the reverse iterator of map,
I have learned how to write on the Internet
First kind

```tree
1020
1053
1043
1086
1102
1079
1090
1094
1106
1004
1064
1099
//Union checking set
1107 Not familiar (check collection)
1118 It's hard to do1107 Same, but I don't know why not
1034
1114
//heap
1147
1155
//chart
1076
1034
1013
1021
1018
1030
1072

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
const int maxn = 205;
const int INF = 0x3f3f3f3f;
using namespace std;
bool vis[maxn]={false};
int main(){
map<int,double,greater<int> > m1;
map<int,double>::iterator it;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int c;
double e;
cin>>c>>e;
m1[c] = e;
}
cin>>n;
for(int i=1;i<=n;i++){
int c;
double e;
cin>>c>>e;
if(m1[c] == 0) m1[c] = e;
else {
m1[c]+=e;
if(m1[c] == 0 ){
it = m1.find(c);
m1.erase(it);
}
}
}
cout<<m1.size();
for(it = m1.begin();it!=m1.end();it++){
printf(" %d %.1f",it->first,it->second);
}
return 0;
}
```

Second kinds

```#include<iostream>
#include<cstring>
#include<vector>
#include<map>
const int maxn = 205;
const int INF = 0x3f3f3f3f;
using namespace std;
bool vis[maxn]={false};
int main(){
map<int,double> m1;
map<int,double>::reverse_iterator rit;
map<int,double>::iterator it;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int c;
double e;
cin>>c>>e;
m1[c] = e;
}
cin>>n;
for(int i=1;i<=n;i++){
int c;
double e;
cin>>c>>e;
if(m1[c] == 0) m1[c] = e;
else {
m1[c]+=e;
if(m1[c] == 0 ){
it = m1.find(c);
m1.erase(it);
}
}
}
cout<<m1.size();
for(rit = m1.rbegin();rit!=m1.rend();rit++){
printf(" %d %.1f",rit->first,rit->second);
}
return 0;
}
```

I feel that I haven't learned a lot about containers, let alone the usage of related containers. I can only slowly find out the shortcomings when I do the questions again. 1003 1004 was done when I learned trees and graphs in order, so I won't write it out alone,

143 original articles published, praised 3, 9228 visitors

Posted by dacio on Mon, 27 Jan 2020 06:43:40 -0800