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
第一次写18分,有一点是自己的疏忽,这题主要是记录一下 to_string的感悟,= =也没啥感悟,就是后来查找错误的时候发现的,感觉很好用。写的时候我觉得转成字符串比较好写,之后再模拟一下就可以了(就模拟的时候有个疏忽),我的模拟和网上大部分不一样,这个模拟思路是我这几天做题积累下来的,其实也没什么
下面都是20分的代码
1.转字符 之前接触的都是 sscanf ,ssprintf 所以做的时候第一时间想到的就是这个,这个掌握了其实很舒服,就比如这道题目,唯一我觉得麻烦的就是参数必须是 char 数组,
#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;
}
第二种 用的to_string,转换的string类型,其实后来比较的时候我想用stringstream,一是不会,二是还没到那个地步,到时候必须用到这种方法再掌握把,我相信后面会有的,也不知道考场上能用to_string不,挺方便的,思路跟我第一个一样
#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;
}
这题就简单模拟和掌握了to_string,后来也看了一下别的大神的思路,也懒得去记录下来的,这种题目还是根据自己第一感觉来吧
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
第一次 17分,怎么说,考虑问题不全面,有多项式相加的时候系数为0的情况,这个时候就要去掉,
我用的是map,
其实这题不是独立做出来的,因为我map的反向迭代器忘记怎么写了,
在网上看了一下 有多学会了一个写法
第一种
树
1020
1053
1043
1086
1102
1079
1090
1094
1106
1004
1064
1099
并查集
1107 不熟(并查集)
1118 做的很难受 感觉和1107 一样但是不知道为什么不行
1034
1114
堆
1147
1155
图
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;
}
第二种
#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;
}
感觉自己容器学的还不是很多,更不要说相关容器的用法了,只能再做题的时候慢慢发现不足了,1003 1004 是我按顺序学习树和图的时候就做了,所以就不单独写出来了,

被折叠的 条评论
为什么被折叠?



