题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
void sort(long int R[],int R_size)
{
for (int i = 1; i < R_size; i++) {
int key = R[i]; //获取要比较的key值
int j = i - 1; //已排序数组中末尾元素下标
while (j >= 0 && R[j] > key){ //从已排序数组中末尾元素开始往前比较
R[j + 1] = R[j]; //元素后移 原本的位置就空出来了
j = j - 1;
}
R[j + 1] = key; //填入那个空出来的位置
}
}
int remove_muti_item(long int R[],int R_size)
{
if(R_size <= 1)
return R_size;
int index = 0;
for (int i = 1; i < R_size; i++) {
if (R[i] != R[index]) {
index++;
R[index] = R[i];
}
}
return index + 1; //返回新数组的长度
}
int main()
{
long int I[1000],R[1000] = {0};
int Index_Set[1000] = {0};
int Index = 0;
int Print_Index = 0;
int I_size,R_size = 0;
int Result_num = 0;
while(scanf("%d",&I_size)!=EOF)
{
for(int i =0;i<I_size;i++)
scanf("%ld",&I[i]);
scanf("%d",&R_size);
for(int i = 0;i<R_size;i++)
scanf("%ld",&R[i]);
/* 排序 */
sort(R,R_size);
/* 去除重复元素 并返回去除后的序列R元素数量*/
R_size = remove_muti_item(R,R_size);
/* 动态开辟一块空间用来存放每个序列R元素被包含的数量 */
int* equal_cnt = (int*)malloc(R_size*sizeof(int));
/* 清空这块空间 */
memset(equal_cnt,0,R_size*sizeof(int));
for(int j = 0;j<R_size;j++) {
for(int i = 0;i<I_size;i++) {
char R_temp[20] = {0};
char I_temp[20] = {0};
sprintf(R_temp,"%ld",R[j]);
sprintf(I_temp,"%ld",I[i]);
if(strstr(I_temp,R_temp))
{
*(equal_cnt + j) += 1;
Result_num += (*(equal_cnt + j) == 1)?4:2;
Index_Set[Index++] = i;
}
}
}
printf("%d ",Result_num);
for(int n = 0;n<R_size;n++) {
if(*(equal_cnt + n))
{
printf("%ld %d ",R[n],*(equal_cnt + n));
for(int m = 0;m<*(equal_cnt + n);m++) {
char I_temp[20] = {0};
sprintf(I_temp,"%ld",I[Index_Set[Print_Index]]);
(Print_Index == Index - 1)?printf("%d %s",Index_Set[Print_Index],I_temp):printf("%d %s ",Index_Set[Print_Index],I_temp);
Print_Index++;
}
}
}
free(equal_cnt);
}
return 0;
}

