Java map AC
import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { /* * 1472 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { Map<Integer, Integer> numMap = new HashMap<Integer, Integer>(); int n1 = scanner.nextInt(); for (int i = 0; i < n1; i++) { int ratioNum = scanner.nextInt(); int indexNum = scanner.nextInt(); int num = 0; if (numMap.get(indexNum) != null) { num = numMap.get(indexNum); } num += ratioNum; numMap.put(indexNum, num); } int n2 = scanner.nextInt(); for (int i = 0; i < n2; i++) { int ratioNum = scanner.nextInt(); int indexNum = scanner.nextInt(); int num = 0; if (numMap.get(indexNum) != null) { num = numMap.get(indexNum); } num += ratioNum; numMap.put(indexNum, num); } Set<Integer> set = numMap.keySet(); int size = set.size(); int array[] = new int[size]; int index = 0; for (Iterator iterator = set.iterator(); iterator.hasNext();) { Integer integer = (Integer) iterator.next(); array[index] = integer; index++; } Arrays.sort(array); StringBuffer sb = new StringBuffer(); for (int i = size-1; i >= 0; i--) { int rationNum = numMap.get(array[i]); if (rationNum != 0) { sb.append(rationNum+" "+array[i]+" "); } } System.out.println(sb.toString().trim()); } } } /************************************************************** Problem: 1472 User: wzqwsrf Language: Java Result: Accepted Time:400 ms Memory:29052 kb ****************************************************************/
Java 数组 AC
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 1472 */ private static int maxn = 2002; private static int addNum = 1000; public static void main(String[] args) throws Exception{ StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int indexArr[] = new int[maxn]; int n1 = (int)st.nval; for (int i = 0; i < n1; i++) { st.nextToken(); int ratioNum = (int)st.nval; st.nextToken(); int indexNum = (int)st.nval; indexArr[indexNum+addNum] += ratioNum; } st.nextToken(); int n2 = (int)st.nval; for (int i = 0; i < n2; i++) { st.nextToken(); int ratioNum = (int)st.nval; st.nextToken(); int indexNum = (int)st.nval; indexArr[indexNum+addNum] += ratioNum; } StringBuffer sb = new StringBuffer(); for (int i = maxn-1; i >= 0; i--) { if (indexArr[i] != 0) { sb.append(indexArr[i]+" "+(i - addNum)+" "); } } System.out.println(sb.toString().trim()); } } } /************************************************************** Problem: 1472 User: wangzhenqing Language: Java Result: Accepted Time:170 ms Memory:24020 kb ****************************************************************/C++ 数组 AC
#include <stdio.h> #include <string.h> const int maxn = 2002; const int addNum = 1000; int n,i,a,b; int indexArr[maxn]; int main(){ while(scanf("%d",&n) != EOF){ memset(indexArr,0,sizeof(indexArr)); for(i = 0; i < n; i++){ scanf("%d %d",&a,&b); indexArr[b+addNum] += a; } scanf("%d",&n); for(i = 0; i < n; i++){ scanf("%d %d",&a,&b); indexArr[b+addNum] += a; } int first = 1; for(i = maxn-1; i >= 0; i--){ if(indexArr[i] != 0){ if(first == 1){ first = 2; printf("%d %d",indexArr[i],i-addNum); }else{ printf(" %d %d",indexArr[i],i-addNum); } } } printf("\n"); } return 0; } /************************************************************** Problem: 1472 User: wangzhenqing Language: C++ Result: Accepted Time:10 ms Memory:1028 kb ****************************************************************/