鱼C论坛

 找回密码
 立即注册
查看: 1363|回复: 1

printf

[复制链接]
发表于 2015-3-21 13:13:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 haiouda 于 2015-3-21 13:18 编辑
  1. /*****************************************************************
  2. 将一个 5*5 的矩阵中最大的元素放在中心,4个角分别放4个最小的元素
  3. (顺序为从左到右,从上到下依次从小到大存放),写一函数实现之。
  4. 用 main函数调用。
  5. *****************************************************************/
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define xx 5     //设置矩阵大小
  9. #define aa 10    //设置矩阵单元大小
  10. int main()
  11. {
  12.         
  13.         char a[xx][xx][aa],*b[xx*xx],*c[xx*xx],*min[4],*max,*max1,*temp1,temp;
  14.         int i,k,p,j;
  15.         
  16.         for (i=0,j=0;i<xx;i++)     //输入矩阵,一个回车代表一个(字符串)单元输入结束
  17.         {
  18.                 for (k=0;k<xx;k++,j++)
  19.                 {
  20.                         b[j]=a[i][k];
  21.                         for(p=0;p<aa;p++)
  22.                         {
  23.                                 temp=getchar();
  24.                                 if(temp=='\n')
  25.                                 {
  26.                                         a[i][k][p]='\0';
  27.                                         break;
  28.                                 }
  29.                                 a[i][k][p]=temp;
  30.                         }
  31.                 }
  32.         }
  33.         
  34.         
  35.         
  36.         
  37.         
  38.         
  39.         min[0]=min[1]=min[2]=min[3]=b[0];     //找出最小的四个数
  40.         
  41.         for(i=0;i<4;i++)                 
  42.         {
  43.                 for(j=0;j<xx*xx;j++)
  44.                 {
  45.                         if(strcmp(min[i],b[j])>0)
  46.                         {
  47.                                 if(i==0)
  48.                                 {                                       
  49.                                         c[j]=b[j];
  50.                                         temp1=min[i];
  51.                                         min[i]=c[j];
  52.                                         c[j]=temp1;      
  53.                                        
  54.                                 }
  55.                                 if(i==1)
  56.                                 {
  57.                                         if(strcmp(b[j],min[i-1])>0)
  58.                                         {
  59.                                                 c[j]=b[j];
  60.                                         temp1=min[i];
  61.                                         min[i]=c[j];
  62.                                         c[j]=temp1;
  63.                                         }
  64.                                 }
  65.                                 if(i==2)
  66.                                 {
  67.                                         if( strcmp(b[j] ,min[i-1])>0)
  68.                                         {
  69.                                                 c[j]=b[j];
  70.                                         temp1=min[i];
  71.                                         min[i]=c[j];
  72.                                         c[j]=temp1;
  73.                                         }
  74.                                 }
  75.                                 
  76.                                 if(i==3)
  77.                                 {
  78.                                         if(strcmp(b[j] ,min[i-1])>0)
  79.                                         {
  80.                         c[j]=b[j];
  81.                                         temp1=min[i];
  82.                                         min[i]=c[j];
  83.                                         c[j]=temp1;
  84.                                         }
  85.                                 }
  86.                         }
  87.                 }                                
  88.         }
  89.         
  90.         
  91.         max=b[0];
  92.     for(j=0;j<xx*xx;j++)      //找出最大的数
  93.         {
  94.                 if(strcmp(max,b[j])<0)
  95.                 {
  96.                         max1=b[j];
  97.                         temp1=max;
  98.                         max=max1;
  99.                         max1=temp1;
  100.                 }
  101.         }
  102.         
  103.         
  104.         printf("\n\n");
  105.         
  106.         for (j=0;j<xx*xx;j++)    //打印原矩阵
  107.         {
  108.                 if(j%xx==0)
  109.                 {
  110.                         printf("\n");
  111.                 }
  112.                 printf("%10s",b[j]);
  113.                
  114.         }


  115. for (j=0;j<xx*xx;j++)        //交换四角及中心单元
  116. {
  117.         if(strcmp(b[j],max)==0)
  118. {
  119.         temp1=b[xx*xx/2];
  120.         b[xx*xx/2]=max;
  121.         b[j]=temp1;
  122. }


  123.         if(strcmp(b[j],min[0])==0)
  124. {
  125.         temp1=b[0];
  126.         b[0]=min[0];
  127.         b[j]=temp1;
  128. }
  129.         if(strcmp(b[j],min[1])==0)
  130. {
  131.         temp1=b[xx-1];
  132.         b[xx-1]=min[1];
  133.         b[j]=temp1;
  134. }
  135.         if(strcmp(b[j],min[2])==0)
  136. {
  137.         temp1=b[xx*xx-xx];
  138.         b[xx*xx-xx]=min[2];
  139.         b[j]=temp1;
  140. }

  141. if(strcmp(b[j],min[3])==0)
  142. {
  143.         temp1=b[xx*xx-1];
  144.         b[xx*xx-1]=min[3];
  145.         b[j]=temp1;
  146. }


  147.         
  148. }        
  149.         printf("\n\n");
  150.         
  151.         for (j=0;j<xx*xx;j++)      //打印交换后的矩阵
  152.         {
  153.                 if(j%xx==0)         printf("\n");
  154.         
  155.                         printf("%10s",b[j]);
  156.                
  157.         }
  158.         
  159.         printf("\n");
  160.         
  161.         return 0;
  162. }




复制代码

第112和162 行改成:printf("%aas",b[j]); 为何不行呀?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-3-21 18:06:15 | 显示全部楼层
因为printf内的不会被弘替换。。。
你作个实验
#define x 10
printf (" x " );
这样你就知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 11:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表