山东大学23强基班计算机导论第七次习题答案

T1.1

int day_of_year(int year,int month,int day)
{
    static int sum[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(year % 4 == 0 && year % 100 != 0)
        sum[2]++;
    else if(year % 4 == 0 && year % 100 ==0 & year %400==0)
        sum[2]++;
    int offset = 0;
    for(int i=1;i<month;i++)
        offset+=sum[i];
    offset+=day;
    return offset;
}

T2.1

#include <stdio.h>


int main()
{
    int sum = 0;
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n;i++)
        for(int j = 0;j < n;j++)
        {
            int t;
            scanf("%d",&t);
            if(i+j==n-1 || i==n-1 || j==n-1)
                continue;
            sum+=t;
        }
    printf("%d",sum);
}

T2.2

#include <stdio.h>

int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;i++){
        int sum=0;
        for(int j=0;j<n;j++){
            int t;
            scanf("%d",&t);
            sum+=t;
        }
        printf("%d\n",sum);
    }
}

T2.3

#include <stdio.h>

#define N 128

int A[N][N];
int B[N][N];
int C[N][N];


int main()
{
    int m1,n1,m2,n2;
    scanf("%d%d",&m1,&n1);
    for(int i=0;i<m1;i++)
        for(int j=0;j<n1;j++)
            scanf("%d",&A[i][j]);
    scanf("%d%d",&m2,&n2);
    for(int i=0;i<m2;i++)
        for(int j=0;j<n2;j++)
            scanf("%d",&B[i][j]);
    if(n1!=m2)
    {
        printf("Error: %d != %d",n1,m2);
        return 0;
    }
    // In this way,It`s will be faster than normal by about 4 times.
    for(int k=0;k<n1;k++)
        for(int i=0;i<m1;i++)
            for(int j=0;j<n2;j++)
                C[i][j]+=A[i][k]*B[k][j];
    printf("%d %d\n",m1,n2);
    for(int i=0;i<m1;i++){
        for(int j=0;j<n2;j++){
            printf("%d",C[i][j]);
            if(j!=n2-1)
                putchar(' ');
        }
        putchar('\n');
    }
}

T2.4

#include <stdio.h>

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int flag = 1; // As a result of buffer.
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                int t;
                scanf("%d",&t);
                if(t!=0 && i>j){
                    // printf("%d %d %d\n",i,j,t);
                    flag=0;
                }
            }
        if(flag)
            puts("YES");
        else
            puts("NO");
    }
}

T2.5

#include <stdio.h>

int main()
{
    int m,n;
    int maxn = -0x01145140;
    int nx,ny;
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
        {
            int t;
            scanf("%d",&t);
            if(t>maxn)
            {
                maxn=t;
                nx=i;ny=j;
            }
        }
    printf("%d\n%d %d",maxn,nx,ny);
    
}

T2.6

#include <stdio.h>

int A[8][8];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            scanf("%d",&A[j][i]);
    for(int i=0;i<n;i++,putchar('\n'))
        for(int j=0;j<n;j++)
            printf("%4d",A[i][j]);
    // So easy.
}

T2.7

#include <stdio.h>

int A[12][12];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            scanf("%d",&A[i][j]);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            int t;scanf("%d",&t);
            if(t!=A[i][j])
            {
                puts("No");
                return 0;
            }
        }
    puts("Yes");
}

T2.8

#include <stdio.h>

int A[12][12];

int main()
{
    int n;
    scanf("%d",&n);
    int dx[]={1,-1,0,0};
    int dy[]={0,0,1,-1};
    // I think it have a math way,But A it will be faster to write.
    int T[]={2,3,1,0};//trans matrix
    int nx=0,ny=0,step=n*n,cur=1,way=0;
    int a=0,b=n-1,c=1,d=n-1; // bound
    while(step--)
    {
        A[ny][nx]=cur;

        if(way==0 && nx==b){
            way=T[way];
            b--;
        }
        else if(way==1 && nx==a){
            way=T[way];
            a++;
        }
        else if(way==2 && ny==d){
            way=T[way];
            d--;
        }
        else if(way==3 && ny==c){
            way=T[way];
            c++;
        }
        
        nx+=dx[way];
        ny+=dy[way];
        cur++;
    }
    // Too ugly
    for(int i=0;i<n;i++,putchar('\n'))
        for(int j=0;j<n;j++)
            printf("%3d",A[i][j]);
        
}

T2.9

#include <stdio.h>


int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++,putchar('\n'))
    {
        for(int j=0;j<n;j++)
        {
            putchar('0' + (i==j));
            putchar(' ');
        }
    }
}
posted @ 2024-05-10 15:42  Icys  阅读(47)  评论(0编辑  收藏  举报