题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int inde; int num; } inde_num; int cmp(const void* a, const void* b) { return *((int*)a) - *((int*)b); } //函数功能:排序并且删除重复项 int move_sort(int k[], int len) { int tem[len]; //使用tem替换原数组进行运算 int count = 0; for (int i = 0; i < len; i++) { tem[i] = k[i]; } qsort(tem, len, sizeof(int), cmp); //使用qsort升序排序 for (int i = 0; i < len - 1; i++) { if (tem[i + 1] == tem[i]) { tem[i] = -1; //排序完后重复的元素标记为-1 count++; } } int inde = 0; for (int i = 0; i < len; i++) { if (tem[i] != -1) { //将不为-1的元素放入原数组 k[inde++] = tem[i]; } } return (len - count); //输出排序降重后的数组长度 } //函数功能:将整数化为字符串,倒序存入a中 int itoa(char a[], int inter) { int i = 0; while (inter != 0) { a[i++] = inter % 10 + '0'; inter = inter / 10; } if(i==0) //特例:如果输入的数字是0 { a[0] = '0'; return 1; } return i; //返回字符串长度 } //函数功能:判断i是否包含r(转化为字符串用strncmp判断) int judge(int i, int r) { char str1[1000] = {0}, str2[1000] = {0}; int n1 = itoa(str1, i); int n2 = itoa(str2, r); int flag = 0; for (int j = 0; j <= n1 - n2; j++) { if (strncmp(str1 + j, str2, n2) == 0) { //如果包含返回1 flag = 1; break; } } return flag; } int main() { int I, R; int I_list[100] = {0}, R_list[100] = {0}; while (~scanf("%d", &I)) { for (int i = 0; i < I; i++) { scanf("%d", &I_list[i]); } scanf("%d", &R); for (int i = 0; i < R; i++) { scanf("%d", &R_list[i]); } int len = move_sort(R_list, R); inde_num res[len][100]; int count[len]; memset(count, 0, sizeof(count)); for (int i = 0; i < len; i++) { int k = 0; for (int j = 0; j < I; j++) { if (judge(I_list[j], R_list[i])) { count[i]++; res[i][k].inde = j; res[i][k++].num = I_list[j]; } } } int sum = 0; for (int j = 0; j < len; j++) { if (count[j] != 0) { sum += 2; sum = sum + 2 * (count[j] ); } } printf("%d ", sum); for (int i = 0; i < len; i++) { if(count[i]!=0) printf("%d %d ", R_list[i], count[i]); for (int j = 0; j < count[i]; j++) { printf("%d %d ", res[i][j].inde, res[i][j].num); } } } }