穿越隧道
AC版
参考大佬思路
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
int n,ka,kb;
double a[N];
int ni;
double ai;
int main(){
cin >> ka;
for(int i = 0; i < ka; i++){
cin >> ni >> ai;
a[ni] = ai;
}
cin >> kb;
for(int i = 0; i < kb; i++){
cin >> ni >> ai;
a[ni] += ai;
}
int cnt = 0;
int i,j;
for(int i = 0; i < 1020; i++){
if(a[i]){
cnt++;
}
}
cout << cnt;
for(int i = 2020; i >= 0; i--){
if(a[i]){
cout << " " << i << " ";
printf("%.1f",a[i]);
}
}
return 0;
}
15scores
失分原因: 1.没有使用printf来保留一位小数,而是直接使用了cout 2.格式错误,行末无空格,或许选择采用空格+数字,会比数字+空格的方式更好
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
typedef pair<int,double> pii;
pii a[N],b[N],ans[N];
int n,ka,kb;
int main(){
cin >> ka;
for(int i = 0; i < ka; i++){
cin >> nn >> ai;
cin >> a[i].x >> a[i].y;
}
sort(a,a + ka);
cin >> kb;
for(int i = 0; i < kb; i++){
cin >> b[i].x >> b[i].y;
}
sort(b,b + kb);
int kk = 0;
int i,j;
for(i = 0, j = 0;i < ka && j < kb; kk++){
if(a[i].x == b[j].x && i < ka && j < kb){
ans[kk].x = a[i].x;
ans[kk].y = a[i].y + b[j].y;
i++,j++;
}
else if(a[i].x < b[j].x && i < ka && j < kb){
ans[kk].x = a[i].x;
ans[kk].y = a[i].y;
i++;
}
else if(a[i].x > b[j].x && i < ka && j < kb){
ans[kk].x = b[j].x;
ans[kk].y = b[j].y;
j++;
}
}
if(i == ka){
while(j < kb){
ans[kk].x = b[j].x;
ans[kk++].y = b[j].y;
j++;
}
}
if(j == kb){
while(i < ka){
ans[kk].x = a[i].x;
ans[kk++].y = a[i].y;
i++;
}
}
sort(ans,ans + kk);
cout << kk <<" ";
for(int m = kk - 1; m >= 0; m--){
cout << ans[m].x <<" " << ans[m].y;
if(m != 0){
cout <<" ";
}
}
return 0;
}
17scores
在15分的基础上,改进了输出数字保留一位小数和输出格式问题,只过了一个点。 错误原因: 解题思路不够简洁,逻辑复杂。
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
int n,ka,kb;
typedef pair<int,double> pii;
pii a[N],b[N],ans[N];
int ni;
double ai;
int main(){
cin >> ka;
for(int i = 0; i < ka; i++){
cin >> a[i].x >> a[i].y;
}
sort(a,a + ka);
cin >> kb;
for(int i = 0; i < kb; i++){
cin >> b[i].x >> b[i].y;
}
sort(b,b + kb);
int cnt = 0;
int i,j;
int kk = 0;
for(i = 0, j = 0;i < ka && j < kb; kk++){
if(a[i].x == b[j].x ){
ans[kk].x = a[i].x;
ans[kk].y = a[i].y + b[j].y;
i++,j++;
}
else if(a[i].x < b[j].x){
ans[kk].x = a[i].x;
ans[kk].y = a[i].y;
i++;
}
else if(a[i].x > b[j].x){
ans[kk].x = b[j].x;
ans[kk].y = b[j].y;
j++;
}
}
if(i == ka){
while(j < kb){
ans[kk].x = b[j].x;
ans[kk++].y = b[j].y;
j++;
}
}
if(j == kb){
while(i < ka){
ans[kk].x = a[i].x;
ans[kk++].y = a[i].y;
i++;
}
}
sort(ans,ans + kk);
cout << kk;
for(int m = kk - 1; m >= 0; m--){
cout << " " << ans[m].x <<" ";
printf("%.1f",ans[m].y);
}
return 0;
}
|