| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 413 人关注过本帖
标题:求助!求讲解!大哥大姐 这个问题有几行没想明白??
只看楼主 加入收藏
autumnyellow
Rank: 2
等 级:论坛游民
帖 子:72
专家分:75
注 册:2015-4-14
结帖率:100%
收藏
已结贴  问题点数:40 回复次数:4 
求助!求讲解!大哥大姐 这个问题有几行没想明白??
/*
将5*5的矩阵中最大的元素放在中心,4个角放最小元素
*/
#include<stdio.h>
int main ()
{
    void change(int * p);
    int a[5][5],*p,i,j;
    printf("please input matrix :\n");
    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
            scanf_s("%d",&a[i][j]);
    p=&a[0][0];
    change(p);
    printf("Now , matrix :\n");
    for(i=0;i<5;i++)
        {
            for(j=0;j<5;j++)
            printf("%5d",a[i][j]);

    printf("\n");
    }
   
    return 0;
}

void change(int *p)
{
    int i,temp,* max,* min,j;
    max=min=p;
    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
            {
                if(*max<*(p+5*i+j))
                max=p+5*i+j;
                if (*min>*(p+5*i+j))
                    min=p+5*i+j;
             }
        temp=*max;//最大的换给中心元素
        *max=*(p+12);
        *(p+12)=temp;
        
        temp=*min;//最小的换给角上
        *min=*p;
        *p=temp;
        min=p+1;
        for(i=0;i<5;i++)
         for(j=0;j<5;j++)
             if(((p+5*i+j)!=p)&&(*min>*(p+5*i+j)))
                 min=p+5*i+j;
        temp=*min;//最小的换给角上
        *min=*(p+4);
        *(p+4)=temp;
        min=p+1;    这个p+1怎么是第三最小值??不是应该用p+2吗?? 它是怎么累加的?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        for(i=0;i<5;i++)
         for(j=0;j<5;j++)
             if(((p+5*i+j)!=p)&&(p+5*i+j)!=(p+4)&&(*min>*(p+5*i+j)))
                 min=p+5*i+j;
        temp=*min;//最小的换给角上
        *min=*(p+20);
        *(p+20)=temp;
            min=p+1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ;
        for(i=0;i<5;i++)
         for(j=0;j<5;j++)
             if(((p+5*i+j)!=p)&&(p+5*i+j)!=(p+4)&&(p+5*i+j)!=(p+20)&&(*min>*(p+5*i+j)))
                 min=p+5*i+j;
        temp=*min;//最小的换给角上
        *min=*(p+24);
        *(p+24)=temp;

}

/*输出

please input matrix :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Now , matrix :
    1    5   21   13    2
    6    7    8    9   10
   11   12   25   14   15
   16   17   18   19   20
    3   22   23   24    4
请按任意键继续. . .



*/

[ 本帖最后由 autumnyellow 于 2015-9-7 23:02 编辑 ]
搜索更多相关主题的帖子: include change please return matrix 
2015-09-07 21:55
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:0 
直接用数组操作不就行了嘛,而且你的p是行指针,直接加减是行的变化

一片落叶掉进了回忆的流年。
2015-09-07 22:16
autumnyellow
Rank: 2
等 级:论坛游民
帖 子:72
专家分:75
注 册:2015-4-14
收藏
得分:0 
回复 2楼 诸葛欧阳
大哥我现在学的就是指针
2015-09-07 22:26
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:40 
你首先要搞清楚你操作的指针代表什么,你的是指向二维数组的指针是行指针

一片落叶掉进了回忆的流年。
2015-09-07 22:28
autumnyellow
Rank: 2
等 级:论坛游民
帖 子:72
专家分:75
注 册:2015-4-14
收藏
得分:0 
回复 4楼 诸葛欧阳
谢谢是我想错了
2015-09-07 23:09
快速回复:求助!求讲解!大哥大姐 这个问题有几行没想明白??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028861 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved