标题: c语言 3*4矩阵,找最大值以及他的行列号 [打印本页]

作者: liuda    时间: 2015-1-22 02:34
标题: c语言 3*4矩阵,找最大值以及他的行列号
  1. //3*4矩阵,找最大值以及他的行列号
  2. //我先把第一个元素放进去,然后挨个比,同时我也把坐标给存起来
  3. //坐标是跟随者最大值更新的
  4. //我想如果矩阵中有几个元素值相等怎么办
  5. #include<stdio.h>
  6. void main()
  7. {int i,j,k;//行号列号临时存储变量
  8. int a[3][4];
  9. printf("please input 12 numbers:\n");
  10. //first将12个元素输入到数组当中去
  11. for(i=0;i<=2;i++)
  12.    {  for(j=0;j<=3;j++)
  13. {scanf("%d",&a[i][j]);}
  14. }//我发现这个循环我用了三遍
  15. //这里输入需要循环的次数与数组中的数的个数相同,否则只会执行一遍
  16. //我需要一个临时存储数据站k,然后开始循环,依次比较12个数

  17.     for(i=0,k=a[0][0];i<=2;i++)
  18. {    for(j=0;j<=3;j++)
  19. {
  20.        if(a[i][j]>=k)
  21. {
  22.     k=a[i][j];
  23. }

  24. }
  25. }
  26.   printf("the biggest number=%d\n",k);
  27.   //now k represents the biggest number
  28. //next,do again
  29.    for(i=0;i<=2;i++)
  30.    {  for(j=0;j<=3;j++)
  31.    {
  32.     if(a[i][j]>=k)
  33. {
  34. printf("%d,%d\n",i,j);
  35. //if there are many same number,my program can solve it too
  36. }
  37. //即便有相等的情况,我们也可以输出它的坐标

  38.    }
  39.    }
  40. }
  41. //一个for循环用了三遍,我头一次
  42. //第一个for是输入元素
  43. //第二个是找出最大值
  44. //最后是输出最大值的坐标
  45. //思路清晰,程序优良
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1