A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© chenhuan_ccit 中级黑马   /  2014-11-1 20:20  /  762 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用c语言循环打印以下数字
1        2        3        4
12        13        14        5
11        16        15        6
10        9        8        7

  1. #include <stdio.h>
  2. #define M 4
  3. void print(int a[][M])
  4. {
  5.         int i,j;
  6.         for(i=0;i<M;i++)
  7.         {
  8.                 for(j=0;j<M;j++)
  9.                 {
  10.                         printf("%4d",a[i][j]);
  11.                 }
  12.                 printf("\n");
  13.         }
  14. }
  15. int main()
  16. {
  17.         int i,j;
  18.         int t=1;
  19.         int a[M][M];
  20.         int h_start=0,h_end=M-1,l_start=0,l_end=M-1;
  21.         int k;
  22.    
  23.         i=0,j=0;
  24.         for(k=0;k<M/2+1;k++)
  25.         {
  26.                 for(j=l_start;j<=l_end;j++)
  27.                 {
  28.                         a[h_start][j] = t++;
  29.                 }
  30.         
  31.                 h_start++;
  32.         
  33.                 for(i=h_start;i<=h_end;i++)
  34.                 {
  35.                         a[i][l_end] = t++;
  36.                 }
  37.         
  38.                 l_end--;
  39.         
  40.                 for(j=l_end;j>=l_start;j--)
  41.                 {
  42.                         a[h_end][j]=t++;
  43.                 }
  44.         
  45.                 h_end--;
  46.         
  47.                 for(i=h_end;i>=h_start;i--)
  48.                 {
  49.                         a[i][l_start]=t++;
  50.                 }
  51.                 l_start++;
  52.         }
  53.        
  54.         print(a);
  55.         return 0;
  56. }
复制代码

2 个回复

倒序浏览
  可以实现吗
回复 使用道具 举报
题目不错
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马