当前位置: 首页 > 工具软件 > mini-code > 使用案例 >

A little code

卓云
2023-12-01
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 4
#define MAX_TOTAL 10
void inputMatrices( int matrix[MAX_SIZE][MAX_SIZE], int size );
void printMatrices( int matrix[MAX_SIZE][MAX_SIZE], int size );
int cmpMatrices(const void *firstMatrixPtr, const void *secondMatrixPtr, void *sizePtr);
int main() {
  size_t total = 0, size = 0;
  scanf("%zu%zu", &total, &size);
  int matrixArr[MAX_TOTAL][MAX_SIZE][MAX_SIZE] = {0};
  for (size_t matrixIndex = 0; matrixIndex < total; ++matrixIndex)
    inputMatrices(matrixArr[matrixIndex], size);
    printf("\n");
  qsort_r(matrixArr, total, sizeof(matrixArr[0]), &cmpMatrices, &size);
  for (size_t matrixIndex = 0; matrixIndex < total; ++matrixIndex)
    //printMatrices(matrixArr[matrixIndex], size);
  return 0;
}
void inputMatrices(int matrixArr[][MAX_SIZE],int size)
{
    int i=0,j=0;
    for(;i<size;i++)
    {
        for(j=0;j<size;j++)
            scanf("%d",&matrixArr[i][j]);
    }
}
void printMatrices(int matrixArr[][MAX_SIZE],int size)
{
    int i=0,j=0;
    for(;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            if(j!=size-1)
                printf("%d ",matrixArr[i][j]);
            else
                printf("%d",matrixArr[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int cmpMatrices(const void *firstMatrixPtr, const void *secondMatrixPtr, void *sizePtr)
{
    size_t mSize=*(size_t *)sizePtr;
    int *mp1=(int *)firstMatrixPtr;
    int *mp2=(int *)secondMatrixPtr;
    int sum1=0,sum2=0,count1=0,count2=0,count3=0;
    count2=mSize-1;


    for(count1=0;count1<mSize;count1++,count2--)
    {
        sum1+=*(mp1+(MAX_SIZE+1)*count1);
        if(mSize!=1)
            sum1+=*(mp1+(MAX_SIZE)*count1+count2);
    }
    for(count1=0,count2=mSize-1;count1<mSize;count1++,count2--)
    {
        sum2+=*(mp2+count1*(MAX_SIZE+1));
        if(mSize!=1)
            sum2+=*(mp2+(MAX_SIZE)*count1+count2);
    }


    if(mSize%2!=0&&mSize!=1)
    {
        sum1-=*(mp1+((MAX_SIZE)*mSize/2)+(mSize/2));
        sum2-=*(mp2+((MAX_SIZE)*mSize/2)+(mSize/2));
    }
    printf("%d %d %d %d\n",mSize,sum1,sum2, (sum1>sum2)-(sum1<sum2));
    return (sum1>sum2)-(sum1<sum2);
}
int cmpMatrices1(const void *firstMatrixPtr, const void *secondMatrixPtr, void *sizePtr) 
{

    size_t matrixSize = *(size_t *)sizePtr;

    int *firstMatrix = (int *)firstMatrixPtr;
    int *secondMatrix = (int *)secondMatrixPtr;

    int sum1 = 0, sum2 = 0;
    int i = 0, j = matrixSize-1;



    for ( i = 0; i < matrixSize; i++, j-- )
    {
        sum1 += *( firstMatrix+(MAX_SIZE+1)*i );
        if ( matrixSize > 1 )
            sum1 += *( firstMatrix+(MAX_SIZE)*i+j );
    }
    for ( i = 0, j = matrixSize-1; i < matrixSize; i++, j-- )
    {
        sum2 += *( secondMatrix+(MAX_SIZE+1)*i );
        if ( matrixSize > 1 )
            sum2 += *( secondMatrix+(MAX_SIZE)*i+j );
    }



     if ( matrixSize%2 != 0 && matrixSize != 1 )
     {
        sum2 -= *( secondMatrix+(MAX_SIZE)*(matrixSize/2)+matrixSize/2 );
        sum1 -= *( firstMatrix+(MAX_SIZE)*(matrixSize/2)+matrixSize/2 );  
    }
    printf("%d %d %d %d\n",matrixSize,sum1,sum2, (sum1>sum2)-(sum1<sum2));
    return (sum1 > sum2) - (sum1 < sum2);
}/*
7 3
-41 43 27
47 10 36
44 20 -28
16 -4 48
22 22 -34
-49 3 -37
-50 -2 4
-20 26 31
-45 -26 -44
-37 27 29
-17 -32 26
35 16 17
-18 -49 44
46 -50 -23
30 -43 0
29 -14 35
-31 32 -17
-47 30 20
-24 -32 0
12 15 -45
50 -22 -48
*/
 类似资料:

相关阅读

相关文章

相关问答