【多比克文学,经典语录,心灵鸡汤,励志语录,正能量句子,www.dopic.net】

数据结构第三周作业(数组、稀疏矩阵、三元组、杨辉魔方)

专题: 散文
作者:Cache_wood 时间:2021-03-23 04:35:33  阅读:253   网上投稿

1.

编写一个算法,计算n阶方阵A的两个对角线元素的和。A采用数组表示。

#include <stdio.h>#define N 4int main(){    int A[N][N];    int i,j,m;    for(i=0;i<N;i++){        for(j=0;j<N;j++){            scanf("%d",&m);            A[i][j]=m;        }    }/*    for(i=0;i<N;i++){        for(j=0;j<N;j++){            printf("%4d",A[i][j]);        }        printf("%d
");    }*/    //计算主对角线的和    int sum1=0,sum2=0;    for(i=0;i<N;i++){        sum1+=A[i][i];    }    //计算副对角线的和    for(i=0;i<N;i++){        sum2+=A[i][N-i-1];    }    printf("The sum of the main diagonals is %d
the sum of the secondary diagonals is %d",sum1,sum2);}    return 0;

2.

编写一个算法,实现两个采用三元组表示的n阶稀疏矩阵的加法运算。

#include <stdio.h>#include <stdlib.h>#include <time.h>#define M 5#define N 6struct part{    int row;    int col;    int value;};int main(){    int A[M][N] ={0};    int B[M][N] ={0};    int C[M][N];    srand(time(NULL));    int n =1 + rand()%(M*N/3); //大致保证非零数在1到10之间    struct part data1[n],data2[n],data3[2*n];    printf("n = %d
",n);    int i,j;    for(i=0;i<n;i++){        A[rand()%M][rand()%N] = 1+rand()%10;        B[rand()%M][rand()%N] = 1+rand()%10;    }    int a=0,b=0,c=0,k=0;    for(i=0;i<M;i++){        for(j=0;j<N;j++){            if(A[i][j]!=0){                data1[a].row = i;                data1[a].col = j;                data1[a].value = A[i][j];                a++;            }            printf("%4d",A[i][j]);        }        printf("
");    }    for(i=0;i<n;i++){        printf("(%d %d %d)
",data1[i].row,data1[i].col,data1[i].value);    }    printf("
");    for(i=0;i<M;i++){        for(j=0;j<N;j++){            if(B[i][j]!=0){                data2[b].row = i;                data2[b].col = j;                data2[b].value = B[i][j];                b++;            }            printf("%4d",B[i][j]);        }        printf("
");    }    for(i=0;i<n;i++){        printf("(%d %d %d)
",data2[i].row,data2[i].col,data2[i].value);    }    for(i=0;i<M;i++){        for(j=0;j<N;j++){            C[i][j] = A[i][j]+B[i][j];            if((A[i][j]!=0)&&(B[i][j]!=0)){                k++;            }        }    }    for(i=0;i<M;i++){        for(j=0;j<N;j++){            if(C[i][j]!=0){                data3[c].row = i;                data3[c].col = j;                data3[c].value = C[i][j];                c++;            }            printf("%4d",C[i][j]);        }        printf("
");    }    printf("k = %d
",k);    for(i=0;i<2*n-k;i++){        printf("(%d %d %d)
",data3[i].row,data3[i].col,data3[i].value);    }    return 0;}n = 6   0   0   0   0   0   0   0   0   0   4   0   0   0   0   0   0   0   0   0   0   0   0   3  10   0   4   0  10   6   0(1 3 4)(3 4 3)(3 5 10)(4 1 4)(4 3 10)(4 4 6)   3   0   0   0   9   0   0   0   7   0   0   0   0   4   0   0   0   0  10   0   0   0   0   0   0   0   0   0   0   4(0 0 3)(0 4 9)(1 2 7)(2 1 4)(3 0 10)(4 5 4)   3   0   0   0   9   0   0   0   7   4   0   0   0   4   0   0   0   0  10   0   0   0   3  10   0   4   0  10   6   4k = 0(0 0 3)(0 4 9)(1 2 7)(1 3 4)(2 1 4)(3 0 10)(3 4 3)(3 5 10)(4 1 4)(4 3 10)(4 4 6)(4 5 4)

3.

编写一个算法,寻找m×n的矩阵中的每一行的最大值元素,并用三元组存储该最大值元素及其位置。

#include <stdio.h>#define M 3#define N 4struct {    int row;    int col;    int value;} data[M];int main(){    int A[M][N] = {1,2,3,4,5,6,7,8,9,10,11,12};    int i,j;    for(i=0;i<M;i++){        for(j=0;j<N;j++){            printf("%4d",A[i][j]);        }        printf("
");    }    for(i=0;i<M;i++){        data[i].row = i;        data[i].col = 0;        data[i].value = A[i][0];    }    for(i=0;i<M;i++){        for(j=0;j<N;j++){            if(A[i][j]>A[i][0]){                data[i].row = i;                data[i].col = j;                data[i].value = A[i][j];            }        }    }    for(i=0;i<M;i++){        printf("(%d %d %d)
",data[i].row,data[i].col,data[i].value);    }    return 0;}

4.

编写一个算法,实现4阶杨辉魔方(阴图)。

#include <stdio.h>#define N 4void swap(int *p,int *q);int main(){    int *p,*q;    int A[N][N] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};    int i,j;    //初始矩阵    for(i=0;i<N;i++){        for(j=0;j<N;j++){            printf("%4d",A[i][j]);        }        printf("
");    }    for(i=0;i<N/2;i++){        for(j=0;j<N;j++){            if((i==j)||(i+j==3)){                p = &A[i][j],q = &A[3-i][3-j];                swap(p,q);            }        }    }    //最终矩阵,用于验证    for(i=0;i<N;i++){        printf("
");        for(j=0;j<N;j++){            printf("%4d",A[i][j]);        }    }    return 0;}void swap(int *p,int *q){    int t;    t = *p;    *p = *q;    *q = t;}

    相关美文阅读: