/**********************************************************************************/
/* Problem: c001 "合并同类项" from */
/* Language: C++ */
/* Result: AC (5ms, 882KB) on SzNOI.cn */
/* Author: Adolf at 2014-12-13 20:24:12 */
/**********************************************************************************/
#include<algorithm>
#include<string>
#include<stdio.h>
#include<iostream>
#define N 100
using namespace std;
struct point {
int x ; //系数
int y ; //指数
}p1[N] ,p2[N],p3[N] ;
int cmp(point a , point b) {
return a.y > b.y ;//将系数从大到小进行排列 ;
}
int main (){
int num1=0 , num2=0 ;
char ch ;
do{
cin >>p1[num1].x >> p1[num1].y ;
num1++ ;
}while( (ch=getchar()) != '\n') ;
do{
cin >>p2[num2].x >> p2[num2].y ;
num2++ ;
}
while( (ch=getchar()) != '\n') ;
sort (p1,p1+num1,cmp) ;//先对两个行列式各自按照指数进行排序
sort (p2,p2+num2, cmp) ;
for(int i =1;i < num1 ;i++){
if(p1[i].y == p1[i-1].y){
p1[i].x += p1[i-1].x ;
p1[i-1].x = 0 ;
p1[i-1].y = 0 ;
}
}
for(int j =1;j<num2 ; j++){
if(p2[j].y == p2[j-1].y) {
p2[j].x += p2[j-1].x ;
p2[j-1].x = p2[j-1].y = 0 ;
}
}
//test
sort(p1,p1+num1,cmp) ;
sort(p2,p2+num2,cmp) ;
for(int i =0;i<num1;i++) {
p3[i] = p1[i] ;
}
for(int j = num1;j<num1+num2 ; j++){
p3[j] = p2[j-num1] ;
}
sort(p3,p3+num1+num2,cmp) ;
for(int j=1;j<num1+num2;j++){
if(p3[j].y == p3[j-1].y) {
p3[j].x += p3[j-1].x ;
p3[j-1].x = p3[j-1].y = 0 ;
}
}
int test = 0 ;
for(int i =0 ;i < num1+num2;i++) {
if(p3[i].x == 0){
continue ;
}
else {
cout << p3[i].x<< " "<<p3[i].y<<" " ;
test ++ ;
}
}
if(test == 0 ) {
cout << "0" ;
}
return 0 ;
}
SzNOI 数据结构 c001
最新推荐文章于 2015-07-24 17:16:36 发布