实验7.2 二维数组

2017-12-01  本文已影响0人  林之禾
7-1 求矩阵的局部极大值(15 分)
#include <stdio.h>
#define ARRAY_SIZE 20

int main()
{
    int m,n,flag=0;
    scanf("%d%d",&m,&n);
    int a[ARRAY_SIZE][ARRAY_SIZE];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
     for(int i=1;i<m-1;i++){
        for(int j=1;j<n-1;j++){
            if(a[i][j] > a[i - 1][j] && a[i][j] > a[i + 1][j] && a[i][j] > a[i][j - 1] && a[i][j] > a[i][j + 1]){
                printf("%d %d %d\n",a[i][j],i+1,j+1);
                flag=1;
            }
        }
    }
    if(flag==0){
        printf("None %d %d",m,n);
    }
    return 0;
}

7-2 求矩阵各行元素之和(15 分)
#include <stdio.h>
#define ARRAY_SIZE 6

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

7-3 计算天数(15 分)
#include <stdio.h>


int main()
{
    int y,m,d,day=0;
    scanf("%d/%d/%d",&y,&m,&d);
    int a[12][2]={1,31,2,28,3,31,4,30,5,31,6,30,7,31,8,31,9,30,10,31,11,30,12,31};
    for(int i=0;i<m-1;i++){
        day+=a[i][1];
    }
    if(y%4==0&&y%100!=0||y%400==0){
        if(m>2){
            day+=1;
        }
    }
    
    printf("%d",day+d);
    return 0;
}


7-4 判断上三角矩阵(15 分)
#include <stdio.h>
#define ARRAY_SIZE 10

int main()
{
    int t,n,sum=0;
    scanf("%d",&t);
    for(int k=0;k<t;k++){
    scanf("%d",&n);
    sum=0;
    int a[ARRAY_SIZE][ARRAY_SIZE];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=1;i<n;i++){
        for(int j=0;j<(i+1)/2;j++){
            sum+=a[i][j];
        }
    }
    if(sum==0){
    printf("YES\n");    
    }else{
        printf("NO\n");
    }   
    }
    
    
    return 0;
}


7-5 打印杨辉三角(20 分)
#include <stdio.h>  

int main(){  
    int a[10][10];  
    int i,j,n;  
    scanf("%d",&n);  
    for(i=0;i<n;++i){  
        a[i][0]=1;  
        a[i][i]=1;  
    }  
    for(i=2;i<n;++i)  
        for(j=1;j<i;j++)  
            a[i][j]=a[i-1][j-1]+a[i-1][j];  
    for(i=0;i<n;++i){  
        for(j=1;j<n-i;++j)  
            printf(" ");  
        for(j=0;j<=i;++j)  
            printf("%4d",a[i][j]);  
        printf("\n"); 
    }  
    return 0;  
}  
7-6 方阵循环右移(20 分)
/*
 ============================================================================
 Name        : 数组循环右移.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

#define MAXN 6


int main() {
    int a[MAXN][MAXN], n, m;
    int i,j,k;
    scanf("%d %d", &m, &n);
    for (i = 0; i < n; i++){
        for (j = 0; j < n; j++){
            
            scanf("%d", &a[i][j]);
            
        }   
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int temp=a[i][n-1];
            for(int k=n-1;k>=0;k--){
                a[i][k]=a[i][k-1];
            }
            a[i][0]=temp;
        }
    }
    for (i = 0; i < n; i++){
        for (j = 0; j < n; j++){
            
            printf("%d ",a[i][j]);
            
        }   
        printf("\n");
    }
    return 0;
}
7-7 找鞍点(20 分)
#include<stdio.h>
int main(){
    int n;
    int i,j,k;
    int num[6][6];
    scanf("%d",&n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&num[i][j]);
        }
    }
    int row,col,max,min;
    for(i=0;i<n;i++){
        max=num[i][0];
        for(j=0;j<n;j++){
            if(max<=num[i][j]){
                max=num[i][j];
                row=j;
            }
        }
        min=max;
        for(k=0;k<n;k++){
            if(min>=num[k][row]){
                min=num[k][row];
                col=k;
            }
        }
        if(max==min){
            break;
        }
        
    }
    if(max==min){
        printf("%d %d",col,row);
    }else{
        printf("NONE");
    }
    
    return 0;
}
7-8 螺旋方阵(20 分)
 
#include <stdio.h>  
#include <stdlib.h> 
#define MAXA 10   
int main()  
{  
    int a[MAXA][MAXA];  
    int n, i = 0, j = 0, loop = 0, count = 1;  
    scanf("%d", &n);  
    while (loop < 1.0*n/2) { 
        do{
            a[i][j] = count++; 
            j++;
        } while(j<n-loop); 
        j--; 
        count--;
        do{
            a[i][j] = count++; 
            i++;
        } while(i<n-loop); 
        i--;  count--;
        do{
            a[i][j] = count++; 
            j--;
        } while(j>=loop); 
        j++; count--;
        do{
            a[i][j] = count++; 
            i--;
        } while(i>loop); 
        i++;  
        count--;
        loop++;  
    }  
    if (n % 2 && n != 1) {  
        a[n/2][n/2] = count;  
    }  
    for (i=0; i<n; i++) {  
        for (j=0; j<n; j++) {  
            printf("%3d", a[i][j]);  
        }  
        printf("\n");  
    }  
      
    return 0; 
}  

上一篇下一篇

猜你喜欢

热点阅读