【华为OD机试】1007 - 合并表记录
华为 记录 合并 机试 OD
2023-09-14 09:05:30 时间
🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
🍂个人博客首页: KJ.JK
💖系列专栏:华为OD机试(Java&Python&C语言)
一、题目
🔸题目描述
数据表记录包含表索引index和数值value(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出
🔸输入输出
输入
先输入键值对的个数n(1 <= n <= 500)
接下来n行每行输入成对的index和value值,以空格隔开
输出
输出合并后的键值对(多行)
🔸样例1
输入
4
0 1
0 2
1 2
3 4
输出
0 3
1 2
3 4
🔸样例2
输入
3
0 1
0 2
8 9
输出
0 3
8 9
二、代码参考
import java.util.*;
public class Main {
public static void main(String agv[]) {
Scanner scanner = new Scanner(System.in);
int tableSize = scanner.nextInt();
int[] key = new int[tableSize]; //用来保存键,方便排序
Map<Integer, Integer> map = new HashMap<>();
int i=0, j=0;
int length = tableSize;
while(tableSize>0){
int k = scanner.nextInt();
int v = scanner.nextInt();
map.put(k, map.getOrDefault(k, 0)+v);
key[i] = k;
i++;
tableSize--;
}
//对key进行从小到大排序
Arrays.sort(key);
for(int k=0;k<length;k++){
if(k==0){
System.out.println(key[k]+" "+map.get(key[k]));
}
else if(key[k]!=key[k-1]){
System.out.println(key[k]+" "+map.get(key[k]));
}
}
}
}
--------------------------------------------------------
'''
思路:
1. 输入形式: [[0, 1], [1, 2], [0, 2], [3, 4]]
2. 对所有index进行快排, [[0, 1], [0, 2], [1, 2], [3, 4]]
3. 对相同index的进行求和, [[0, 3], [1, 2], [3, 4]]
4. 输出形式
'''
# 输入形式
import sys
hash = [] # [[0, 1], [0, 2], [1, 2], [3, 4]]
a = int(input().strip())
for i in range(a):
cur_list = input().strip().split(' ')
hash.append(list(map(int, cur_list)))
# 快排
def part(hash, l, r):
pivot = hash[l]
while l < r:
while l < r and hash[r][0] >= pivot[0]:
r -= 1
if hash[r][0] < pivot[0]:
hash[l] = hash[r]
while l < r and hash[l][0] <= pivot[0]:
l += 1
if hash[l][0] > pivot[0]:
hash[r] = hash[l]
if l==r:
hash[l] = pivot
mid = l
return mid
def quick_sort(hash, left, right):
if left > right:
return
mid = part(hash, left, right)
quick_sort(hash, left, mid-1)
quick_sort(hash, mid+1, right)
quick_sort(hash, left=0, right=len(hash)-1)
# 求和
sum_ = []
cur_sum = hash[0]
for i in hash[1:]:
if i[0] == cur_sum[0]:
cur_sum = [cur_sum[0], i[1]+cur_sum[1]]
else:
sum_.append(cur_sum)
cur_sum = i
sum_.append(cur_sum)
# print(sum_)
# 转变形式
res = ''
for sublst in sum_:
res+= ' '.join([str(s) for s in sublst]) + '\n'
print(res.strip())
--------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int key;
int value;
} Dictionary;
int Partition(Dictionary* a, int low, int high) {
//int pivot=a[low].key,p_value=a[low].value;
Dictionary pivot = a[low];
while (low < high) {
while (low < high && a[high].key >= pivot.key) --high;
a[low] = a[high];
while (low < high && a[low].key <= pivot.key) ++low;
a[high] = a[low];
}
a[low] = pivot;
return low;//返回枢轴元素最终位置
}
void QuickSort(Dictionary* a, int low, int high) {
if (low < high) {
int pivotpos = Partition(a, low, high);
QuickSort(a, low, pivotpos - 1);
QuickSort(a, pivotpos + 1, high);
}
}
int main() {
int n, a, b;
Dictionary* dic;
scanf("%d", &n);
dic = (Dictionary*)calloc(n, sizeof(Dictionary));
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
dic[i].key = a;
dic[i].value = b;
}
QuickSort(dic, 0, n - 1);
//qsort(dic, n, sizeof(Dictionary), compare);
int k, v;
k = dic[0].key;
v = dic[0].value;
for (int i = 0; i < n; i++) {
if ((i + 1) < n && dic[i].key == dic[i + 1].key) //如果键相同
v = v + dic[i + 1].value; //则值相加
else {
printf("%d %d\n", k, v);
if ((i + 1) < n) {
k = dic[i + 1].key;
v = dic[i + 1].value;
}
}
}
return 0;
}
作者:KJ.JK
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习