题解 | #数据分类处理#, 此题不麻烦就是太繁琐了
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
#include <stdio.h> #include <string.h> typedef struct{ char idx; char str[16]; unsigned int num; }NOD; void orderArry(NOD * arr, int len) { NOD tmp; for(int i = 0; i < len; i++) { for(int j = i; j <len; j++) { if(arr[i].num > arr[j].num) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } } } int createR(NOD *s, NOD *t, int sLen) { unsigned int tmp = 0; int tLen = 0; for(int i = 0; i < sLen; i++) { if(0 == tLen) { t[0] = s[0]; tLen++; }else{ if(t[tLen-1].num != s[i].num) { t[tLen] = s[i]; tLen++; } } } return tLen; } int main() { int INum = 0; NOD Is[1000] = {0}; scanf("%d",&INum); for(int i = 0; i < INum; i++) { scanf("%d",&Is[i].num); Is[i].idx = i; sprintf(Is[i].str,"%d",Is[i].num); } int RNum = 0; NOD Rs[1000] = {0}; scanf("%d",&RNum); for(int i = 0; i < RNum; i++) { scanf("%d",&Rs[i].num); Rs[i].idx = i; sprintf(Rs[i].str,"%d",Rs[i].num); } //排序 Rs orderArry(Rs, RNum); int RsRLen = 0; NOD RsR[1000] = {0}; RsRLen = createR(Rs, RsR, RNum); char RsRFind[1000] = {0}; int findFlag = 0; char* findIndex = (char*)malloc(INum*RsRLen *2); int findAllCount = 0; int findRCount = 0; for(int i = 0; i < RsRLen; i++) { for(int j = 0; j< INum; j++) { if(NULL != strstr(Is[j].str,RsR[i].str)) { findFlag = 1; findIndex[findAllCount++] = j; RsRFind[i]++; } } if(findFlag){ findRCount++; } findFlag = 0; } int AllNum = findRCount*2 + 2*findAllCount; printf("%d ",AllNum); int indexTemp = 0; for(int i = 0; i < RsRLen; i++) { if(RsRFind[i] > 0){ printf("%d %d ",RsR[i].num,RsRFind[i]); for(int j = indexTemp; (j < indexTemp + RsRFind[i]) && (j < findAllCount); j++) { printf("%d %d ",Is[findIndex[j]].idx,Is[findIndex[j]].num); } indexTemp += RsRFind[i]; } } free(findIndex); return 0; }