#include <stdio.h>
int statistics[232][4];
int statsKeyFrontPos[232][3];
int statsKeyMiddlePos[232][5];
int statsKeyBackPos[232][5];
int lastStat = 0;
int i;
size_t stat_exists(int len, int *stat, int *el, int max, int *check)
{
int c;
for (c = 0; c < len; c++) {
if (stat[c] != el[c]) {
*check = 0;
return 1;
}
}
return 0;
}
void insert_stat(int len, int *stat, int *el, int max)
{
int c;
while (max) {
if (len == max--) {
for (c = 0; c < len; c++) {
stat[c] = el[c];
}
}
}
}
void bubble_sort(int *list, int n)
{
int c, d, t;
for (c = 0 ; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (list[d] > list[d+1]) {
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
int main()
{
int lenFront, lenMiddle, lenBack;
int frontPos[3], middlePos[5], backPos[5];
int checkFront, checkMiddle, checkBack;
int isInList;
int c;
// +-------------------+
// |First loop |
// +-------------------+
lenFront = 1;
lenMiddle = 2;
lenBack = 2;
// 5 || 2,1 || 3,4
frontPos[0] = 5;
middlePos[0] = 2;
middlePos[1] = 1;
backPos[0] = 3;
backPos[1] = 4;
bubble_sort(frontPos, lenFront);
bubble_sort(middlePos, lenMiddle);
bubble_sort(backPos, lenBack);
isInList = 0;
for (i = 0; i < lastStat; i++) {
checkFront = checkMiddle = checkBack = 1;
if (stat_exists(lenFront , statsKeyFrontPos[i] , frontPos , 3, &checkFront ) ||
stat_exists(lenMiddle, statsKeyMiddlePos[i], middlePos, 5, &checkMiddle) ||
stat_exists(lenBack , statsKeyBackPos[i] , backPos , 5, &checkBack))
{
continue;
}
if (checkFront == 1 && checkMiddle == 1 && checkBack == 1) {
isInList = 1;
statistics[i][0]++;
statistics[i][1]++;
statistics[i][2]++;
statistics[i][3]++;
break;
}
}
if (!isInList)
{
insert_stat(lenFront , statsKeyFrontPos[lastStat] , frontPos , 3);
insert_stat(lenMiddle, statsKeyMiddlePos[lastStat], middlePos, 5);
insert_stat(lenBack , statsKeyBackPos[lastStat] , backPos , 5);
statistics[lastStat][0]++;
statistics[lastStat][1]++;
statistics[lastStat][2]++;
statistics[lastStat][3]++;
lastStat++;
}
// +-------------------+
// |Second loop |
// +-------------------+
lenFront = 3;
lenMiddle = 2;
lenBack = 0;
// 9,8,7 || 12,11 || 0,0
frontPos[0] = 9;
frontPos[1] = 8;
frontPos[2] = 7;
middlePos[0] = 12;
middlePos[1] = 11;
backPos[0] = 0;
backPos[1] = 0;
bubble_sort(frontPos, lenFront);
bubble_sort(middlePos, lenMiddle);
bubble_sort(backPos, lenBack);
isInList = 0;
for (i = 0; i < lastStat; i++) {
checkFront = checkMiddle = checkBack = 1;
if (stat_exists(lenFront , statsKeyFrontPos[i] , frontPos , 3, &checkFront ) ||
stat_exists(lenMiddle, statsKeyMiddlePos[i], middlePos, 5, &checkMiddle) ||
stat_exists(lenBack , statsKeyBackPos[i] , backPos , 5, &checkBack))
{
continue;
}
if (checkFront == 1 && checkMiddle == 1 && checkBack == 1) {
isInList = 1;
statistics[i][0]++;
statistics[i][1]++;
statistics[i][2]++;
statistics[i][3]++;
break;
}
}
if (!isInList)
{
insert_stat(lenFront , statsKeyFrontPos[lastStat] , frontPos , 3);
insert_stat(lenMiddle, statsKeyMiddlePos[lastStat], middlePos, 5);
insert_stat(lenBack , statsKeyBackPos[lastStat] , backPos , 5);
statistics[lastStat][0]++;
statistics[lastStat][1]++;
statistics[lastStat][2]++;
statistics[lastStat][3]++;
lastStat++;
}
// +-------------------+
// |Third loop |
// +-------------------+
lenFront = 1;
lenMiddle = 2;
lenBack = 2;
// 5 || 1,2 || 4,3
frontPos[0] = 5;
middlePos[0] = 1;
middlePos[1] = 2;
backPos[0] = 4;
backPos[1] = 3;
bubble_sort(frontPos, lenFront);
bubble_sort(middlePos, lenMiddle);
bubble_sort(backPos, lenBack);
isInList = 0;
for (i = 0; i < lastStat; i++) {
checkFront = checkMiddle = checkBack = 1;
if (stat_exists(lenFront , statsKeyFrontPos[i] , frontPos , 3, &checkFront ) ||
stat_exists(lenMiddle, statsKeyMiddlePos[i], middlePos, 5, &checkMiddle) ||
stat_exists(lenBack , statsKeyBackPos[i] , backPos , 5, &checkBack))
{
continue;
}
if (checkFront == 1 && checkMiddle == 1 && checkBack == 1) {
isInList = 1;
statistics[i][0]++;
statistics[i][1]++;
statistics[i][2]++;
statistics[i][3]++;
break;
}
}
if (!isInList)
{
insert_stat(lenFront , statsKeyFrontPos[lastStat] , frontPos , 3);
insert_stat(lenMiddle, statsKeyMiddlePos[lastStat], middlePos, 5);
insert_stat(lenBack , statsKeyBackPos[lastStat] , backPos , 5);
statistics[lastStat][0]++;
statistics[lastStat][1]++;
statistics[lastStat][2]++;
statistics[lastStat][3]++;
lastStat++;
}
// Print results
for (i = 0; i < lastStat; i++)
{
printf("Front: %d %d %d\n", statsKeyFrontPos[i][0], statsKeyFrontPos[i][1], statsKeyFrontPos[i][2]);
printf("Middle: %d %d %d\n", statsKeyMiddlePos[i][0], statsKeyMiddlePos[i][1], statsKeyMiddlePos[i][2]);
printf("Back: %d %d %d\n", statsKeyBackPos[i][0], statsKeyBackPos[i][1], statsKeyBackPos[i][2]);
printf("Statistics: %d %d %d %d\n", statistics[i][0], statistics[i][1], statistics[i][2], statistics[i][3]);
printf("\n\n");
}
return 0;
}
Output:
Front: 5 0 0
Middle: 1 2 0
Back: 3 4 0
Statistics: 2 2 2 2
Front: 7 8 9
Middle: 11 12 0
Back: 0 0 0
Statistics: 1 1 1 1
Ho risolto così, ma se qualcuno ha suggerimenti da dare è ben accetto.