nefu瑞格数组
–题目来源-nefu瑞格
4188
C
4189
A
4198
#include <stdio.h>
#include <malloc.h>
void LatinMatrix(int **a,int n)
{
// write your code here
int i,j;
for(i=0;i<n;i++)
{
a[0][i]=i+1;
a[i][0]=i+1;
}
for(i=1;i<n;i++)
for(j=1;j<n;j++)
{
if(a[i][j-1]<n)
a[i][j]=a[i][j-1]+1;
else
a[i][j]=1;
}
}
void PrintMatrix(int **a,int n)
{
int i,j;
for (i=0;i<n;i++) {
for (j=0;j<n;j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
int main()
{
int n,i;
scanf("%d", &n);
int **array = (long **)malloc(n*sizeof(long));
for (i=0;i<n;i++)
array[i] = (long *)malloc(n*sizeof(long));
LatinMatrix(array,n);
PrintMatrix(array,n);
return 0;
}
4197
#include <stdio.h>
#include <malloc.h>
void swapRow(int **a,int n,int i,int j,float *ave)
{
// write your code here
//int *p;
int m,t;
float s;
if(ave[i]>ave[j])
{
/* p=*a[i];
*a[i]=*a[j];
*a[j]=p;*/
for(m=0;m<n;m++)
{
t=a[i][m];
a[i][m]=a[j][m];
a[j][m]=t;
}
s=ave[i];
ave[i]=ave[j];
ave[j]=s;
}
}
void PrintMatrix(int **a,int n)
{
int i,j;
for (i=0;i<n;i++) {
for (j=0;j<n;j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
void Arrange(int **a,int n)
{
// write your code here
int i,j;
float ave[n];
for(i=0;i<n;i++)
ave[i]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
ave[i]=ave[i]+a[i][j];
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
swapRow(a,n,i,j,ave);
}
PrintMatrix(a,n);
}
int main()
{
int n,i,j;
scanf("%d", &n);
int **array = (long **)malloc(n*sizeof(long));
for (i=0;i<n;i++)
array[i] = (long *)malloc(n*sizeof(long));
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
scanf("%d", array[i]+j);
}
}
Arrange(array,n);
return 0;
}