|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 haiouda 于 2015-3-21 13:18 编辑
- /*****************************************************************
- 将一个 5*5 的矩阵中最大的元素放在中心,4个角分别放4个最小的元素
- (顺序为从左到右,从上到下依次从小到大存放),写一函数实现之。
- 用 main函数调用。
- *****************************************************************/
- #include <stdio.h>
- #include <string.h>
- #define xx 5 //设置矩阵大小
- #define aa 10 //设置矩阵单元大小
- int main()
- {
-
- char a[xx][xx][aa],*b[xx*xx],*c[xx*xx],*min[4],*max,*max1,*temp1,temp;
- int i,k,p,j;
-
- for (i=0,j=0;i<xx;i++) //输入矩阵,一个回车代表一个(字符串)单元输入结束
- {
- for (k=0;k<xx;k++,j++)
- {
- b[j]=a[i][k];
- for(p=0;p<aa;p++)
- {
- temp=getchar();
- if(temp=='\n')
- {
- a[i][k][p]='\0';
- break;
- }
- a[i][k][p]=temp;
- }
- }
- }
-
-
-
-
-
-
- min[0]=min[1]=min[2]=min[3]=b[0]; //找出最小的四个数
-
- for(i=0;i<4;i++)
- {
- for(j=0;j<xx*xx;j++)
- {
- if(strcmp(min[i],b[j])>0)
- {
- if(i==0)
- {
- c[j]=b[j];
- temp1=min[i];
- min[i]=c[j];
- c[j]=temp1;
-
- }
- if(i==1)
- {
- if(strcmp(b[j],min[i-1])>0)
- {
- c[j]=b[j];
- temp1=min[i];
- min[i]=c[j];
- c[j]=temp1;
- }
- }
- if(i==2)
- {
- if( strcmp(b[j] ,min[i-1])>0)
- {
- c[j]=b[j];
- temp1=min[i];
- min[i]=c[j];
- c[j]=temp1;
- }
- }
-
- if(i==3)
- {
- if(strcmp(b[j] ,min[i-1])>0)
- {
- c[j]=b[j];
- temp1=min[i];
- min[i]=c[j];
- c[j]=temp1;
- }
- }
- }
- }
- }
-
-
- max=b[0];
- for(j=0;j<xx*xx;j++) //找出最大的数
- {
- if(strcmp(max,b[j])<0)
- {
- max1=b[j];
- temp1=max;
- max=max1;
- max1=temp1;
- }
- }
-
-
- printf("\n\n");
-
- for (j=0;j<xx*xx;j++) //打印原矩阵
- {
- if(j%xx==0)
- {
- printf("\n");
- }
- printf("%10s",b[j]);
-
- }
- for (j=0;j<xx*xx;j++) //交换四角及中心单元
- {
- if(strcmp(b[j],max)==0)
- {
- temp1=b[xx*xx/2];
- b[xx*xx/2]=max;
- b[j]=temp1;
- }
- if(strcmp(b[j],min[0])==0)
- {
- temp1=b[0];
- b[0]=min[0];
- b[j]=temp1;
- }
- if(strcmp(b[j],min[1])==0)
- {
- temp1=b[xx-1];
- b[xx-1]=min[1];
- b[j]=temp1;
- }
- if(strcmp(b[j],min[2])==0)
- {
- temp1=b[xx*xx-xx];
- b[xx*xx-xx]=min[2];
- b[j]=temp1;
- }
- if(strcmp(b[j],min[3])==0)
- {
- temp1=b[xx*xx-1];
- b[xx*xx-1]=min[3];
- b[j]=temp1;
- }
-
- }
- printf("\n\n");
-
- for (j=0;j<xx*xx;j++) //打印交换后的矩阵
- {
- if(j%xx==0) printf("\n");
-
- printf("%10s",b[j]);
-
- }
-
- printf("\n");
-
- return 0;
- }
复制代码
第112和162 行改成:printf("%aas",b[j]); 为何不行呀?
|
|