数据表记录包含表索引index和数值value(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出。
提示:
0 <= index <= 11111111
1 <= value <= 100000
先输入键值对的个数n(1 <= n <= 500)
接下来n行每行输入成对的index和value值,以空格隔开
输出合并后的键值对(多行)
4 0 1 0 2 1 2 3 4
0 3 1 2 3 4
3 0 1 0 2 8 9
0 3 8 9
#include <stdio.h> struct Book { int index; int value; }; //结构体升序排列 void B_sort(struct Book* nums, int numsSize) { int flag = 1; int tempindex; int tempvalue; for (int i = 0; i < numsSize - 1; i++) { for (int j = numsSize - 1; j > i; j--) { if (nums[j].index < nums[j - 1].index) { tempindex = nums[j - 1].index; tempvalue = nums[j - 1].value; nums[j - 1].index = nums[j].index; nums[j - 1].value = nums[j].value; nums[j].index = tempindex; nums[j].value = tempvalue; flag = 0; } } if (flag==1) { break; } } } int main() { int n; scanf("%d", &n); struct Book arr[n]; int i = 0; while (i < n) { scanf("%d %d", &arr[i].index, &arr[i].value); i++; } //排序 B_sort(arr, n); //从小到大排序在开始 for (i = 0; i < n - 1; i++) { if (arr[i].index == arr[i + 1].index) { arr[i].value = arr[i].value + arr[i + 1].value; for (int j = i + 1; j < n - 1; j++) { arr[j].index = arr[j + 1].index; arr[j].value = arr[j + 1].value; } n = n - 1; //防止多个重复 i=i-1; } } for (int k = 0; k < n; k++) { printf("%d %d\n", arr[k].index, arr[k].value); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <stdint.h> #include <stdbool.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #define HASHMAP_CAPACITY 10 typedef int KeyType; typedef int ValueType; typedef struct node_s { int key; int val; struct node_s* next; } KeyValueNode; typedef struct { KeyValueNode* buckets[HASHMAP_CAPACITY]; uint32_t hash_seed; } HashMap; int tmp[500] = { 0 };//用于存储哈希表结点的key值 int count = 0; //用于对哈希表结点计数 //哈希函数 static uint32_t hash(const void* key, int len, uint32_t seed) { const uint32_t m = 0x5bd1e995; const int r = 24; uint32_t h = seed ^ len; const unsigned char* data = (const unsigned char*)key; while (len >= 4) { uint32_t k = *(uint32_t*)data; k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; data += 4; len -= 4; } switch (len) { case 3: h ^= data[2] << 16; case 2: h ^= data[1] << 8; case 1: h ^= data[0]; h *= m; }; h ^= h >> 13; h *= m; h ^= h >> 15; return h; } //创建哈希表 HashMap* hashmap_create() { HashMap* map = calloc(1, sizeof(HashMap)); if (map == NULL) { printf("error: calloc failed in hashmap_create.\n"); exit(-1); } map->hash_seed = time(NULL); return map; } //销毁哈希表 void hashmap_destroy(HashMap* map) { for (size_t i = 0; i < HASHMAP_CAPACITY; i++) { KeyValueNode* curr = map->buckets[i]; while (curr != NULL) { KeyValueNode* tmp = curr->next; free(curr); curr = tmp; } } free(map); } //插入一个键值对 ValueType hashmap_put(HashMap* map, int key, int val) { char key_str[sizeof(int)]; memcpy(key_str, &key, sizeof(int)); int idx = hash(key_str, sizeof(int), map->hash_seed) % HASHMAP_CAPACITY; KeyValueNode* curr = map->buckets[idx]; while (curr != NULL) { if (curr->key == key) { int old_val = curr->val; curr->val = old_val + val; return old_val; } curr = curr->next; } KeyValueNode* new_node = malloc(sizeof(KeyValueNode)); if (new_node == NULL) { printf("Error: malloc failed in hashmap_put.\n"); exit(1); } new_node->key = key; new_node->val = val; new_node->next = map->buckets[idx]; map->buckets[idx] = new_node; return 0; } // 查询一个键值对 ValueType hashmap_get(HashMap* map, KeyType key) { char key_str[sizeof(int)]; memcpy(key_str, &key, sizeof(int)); int idx = hash(key_str, sizeof(int), map->hash_seed) % HASHMAP_CAPACITY; KeyValueNode* curr = map->buckets[idx]; while (curr != NULL) { if (curr->key == key) { return curr->val; } curr = curr->next; } return 0; // 若不存在则返回0 } // 快速排序的分区函数 static int partition(int arr[], int left, int right) { int pivot = arr[left]; int low = left, high = right; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] < pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } // 递归函数 void quick_sort_recursive(int arr[], int left, int right) { if (left < right) { int pivot_index = partition(arr, left, right); quick_sort_recursive(arr, left, pivot_index - 1); quick_sort_recursive(arr, pivot_index + 1, right); } } // 快速排序 void quick_sort(int arr[], int len) { quick_sort_recursive(arr, 0, len - 1); } int main(void) { HashMap* map = hashmap_create(); if (map == NULL) { printf("哈希表创建失败。\n"); return 1; } int sum; scanf("%d", &sum); int key[500] = { 0 }, value[500] = { 0 }; for (int i = 0; i < sum; i++) { scanf("%d %d", &key[i], &value[i]); } for (int i = 0; i < sum; i++) { hashmap_put(map, key[i], value[i]); } for (int i = 0; i < HASHMAP_CAPACITY; i++) { KeyValueNode* curr = map->buckets[i]; while (curr != NULL) { tmp[count++] = curr->key; curr = curr->next; } } quick_sort(tmp, count); for (int i = 0; i < count; i++) { printf("%d %d\n", tmp[i], hashmap_get(map, tmp[i])); } hashmap_destroy(map); return 0; }
#define CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> //功能:打印二维数组 void print_2D_array(int kv[][2], int rows) { int i = 0; for (i = 0; i < rows; i++) { printf("%d %d\n", kv[i][0], kv[i][1]); } } /* * @brief:删除二维数组里第二列是0的行。 * @note:行数肯定会减少,所以要使用指针更新原rows。 */ void proc_del_zero(int kv[][2], int* rows) { int i = 0, new_rows = 0; for (i = 0; i < *rows; i++) { if (kv[i][1] != 0) { kv[new_rows][1] = kv[i][1]; kv[new_rows][0] = kv[i][0]; new_rows++; } } *rows = new_rows; } /* * @brief:将相同索引的数值进行求和运算 * @param:**kv:待处理的二维数组 * @param:rows:待处理的二维数组的行数,也是键值对的个数 * @retval:NONE * @note:第0列是key,第1列是value。 */ void proc_sum(int kv[][2], int rows) { int i = 0, j = 0; for (i = 0; i < rows; i++) { for (j = i + 1; j < rows; j++) { if (kv[i][0] == kv[j][0]) { kv[i][1] += kv[j][1]; //求和 kv[j][1] = 0; //被加过的元素置零,防止多次被加。 } } } } /* * @brief:按照key值升序排列二维数组 * @param:kv[][2]:待处理的二维数组 * @param:rows:待处理的二维数组的行数,也是键值对的个数 * @retval:NONE * @note:第0列是key,第1列是value。本次使用选择排序算法。 */ void proc_upsort(int kv[][2], int rows) { // 检查数组是否有元素 if (rows <= 0) return; int i = 0, j = 0; int min = 0; int temp[1][2] = {0}; for (i = 0; i < rows; i++) { min = i; for (j = i + 1; j < rows; j++) { if (kv[min][0] > kv[j][0]) { min = j; //拿到最小key值对应的行数字 } } //交换kv[i]行的数据和kv[min]行的数据 temp[0][0] = kv[i][0]; temp[0][1] = kv[i][1]; kv[i][0] = kv[min][0]; kv[i][1] = kv[min][1]; kv[min][0] = temp[0][0]; kv[min][1] = temp[0][1]; } } int main() { int couples = 0; //键值对的个数,也是二维数组的行数。 int key_value[501][2] = {0}; //根据题目,做多500行2列,第一列键,第二列值。 int i = 0; scanf("%d", &couples); if (couples < 1 || couples > 500) //键值对的个数必须1 <= n <= 500 exit(-1); for (i = 0; i < couples; i++) { //循环输入键值对 scanf("%d %d", &key_value[i][0], &key_value[i][1]); if (key_value[i][0] < 0 || key_value[i][0] > 11111111) exit(-1); //必须 0 <= index <= 11111111 if (key_value[i][1] < 1 || key_value[i][1] > 100000) exit(-1); //必须 1 <= value <= 100000 } proc_sum(key_value, couples); //键相同的行,对值求和,求完和的行把其value清零。 proc_del_zero(key_value, &couples); //删除二维数组里第二列是0的行 proc_upsort(key_value, couples); //做完前面的步骤后,把每一行按照key的值升序 print_2D_array(key_value, couples); //打印二维数组 return 0; }
#include "stdio.h" #include "string.h" #include <ctype.h> //思路:分成三个部分,求和,排序,输出。注意测试用例里给的数据是乱序所以必须排序。 //这个代码有一个问题,就是循环相加的部分只能处理index只有两个相同的部分,三个及以上就会漏,不知道怎么回事和怎么补救,还好例子里最多三个,因此用了两遍就过了。 void upper_bubble_sort_for_IA(int index[], int value[],int n) { int i,j,tempv,tempi; for(i = 0; i <n; i++) { for(j = 0; j < n - 1- i; j++) { if(index[j] > index[j+1]) { tempv = value[j]; value[j] = value[j+1]; value[j+1] = tempv; tempi = index[j]; index[j] = index[j+1]; index[j+1] = tempi; } } } } int main() { //输入部分 int n,i,a; //struct IA inva; scanf("%d",&n); int index[500] = {0},value[500] = {0}; for(i = 0; i < n; i++) { scanf("%d",&index[i]); scanf(" %d", &value[i]); } //比较并求和部分 for (i = 0; i < n; i++) { for(a = 1; a < n - 1 -i; a++) { if(index[i] == index[i+a]&& value[i+a] !=0) { value[i] = value[i] + value[i+a]; value[i+a] = 0; } } } //排序 upper_bubble_sort_for_IA(index,value,n); for (i = 0; i < n; i++) { for(a = 1; a < n -i; a++) { if(index[i] == index[i+a]&& value[i+a] !=0) { value[i] = value[i] + value[i+a]; value[i+a] = 0; //a = 1; //i = 0; } } } //输出 for(i = 0; i < n; i++) { if (value[i] !=0) { printf("%d %d\n",index[i],value[i]); } } }
#include <stdio.h> int main() { int count=0,vari=0,i=0,j=0,index=0; scanf("%d",&count); long int a[500][2],b[500][2]; for(i=0;i<count;i++) { scanf("%ld %ld",&a[i][0],&a[i][1]); } for(i=0;i<count;i++){ for(j=0;j<count;j++){ if(a[i][0]==b[j][0]) break; } if(j>=vari) { index = vari; b[index][0] = a[i][0]; b[index][1] = a[i][1]; vari++; }else{ index = j; b[index][1] = b[index][1]+a[i][1]; } } long int temp[2]; for(i=0;i<vari-1;i++){ for(j=0;j<vari-1-i;j++){ if(b[j][0]>b[j+1][0]){ temp[0] = b[j][0]; temp[1] = b[j][1]; b[j][0] = b[j+1][0]; b[j][1] = b[j+1][1]; b[j+1][0] = temp[0]; b[j+1][1] = temp[1]; } } } for(j=0;j<vari;j++){ printf("%ld %ld\n",b[j][0],b[j][1]); } return 0; }
#include <stdio.h> int main() { int index[501]={-1}, value[501]={-1},i,j,n,temp1,temp2; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d %d",&index[i],&value[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(index[i]==index[j]) { value[i]=value[i]+value[j]; index[j]=-1; } } } for(i=0;i<n-1;i++) { for(j=0;j<n-1;j++) { if(index[j]>index[j+1]) { temp1=index[j]; temp2=value[j]; index[j]=index[j+1]; value[j]=value[j+1]; index[j+1]=temp1; value[j+1]=temp2; } } } for(i=0;i<n;i++) { if(index[i]>=0) printf("%d %d\n",index[i],value[i]); } return 0; }