|
- //3*4矩陣,找最大值以及他的行列號
- //我先把第一個元素放進去,然后挨個比,同時我也把坐標(biāo)給存起來
- //坐標(biāo)是跟隨者最大值更新的
- //我想如果矩陣中有幾個元素值相等怎么辦
- #include<stdio.h>
- void main()
- {int i,j,k;//行號列號臨時存儲變量
- int a[3][4];
- printf("please input 12 numbers:\n");
- //first將12個元素輸入到數(shù)組當(dāng)中去
- for(i=0;i<=2;i++)
- { for(j=0;j<=3;j++)
- {scanf("%d",&a[i][j]);}
- }//我發(fā)現(xiàn)這個循環(huán)我用了三遍
- //這里輸入需要循環(huán)的次數(shù)與數(shù)組中的數(shù)的個數(shù)相同,否則只會執(zhí)行一遍
- //我需要一個臨時存儲數(shù)據(jù)站k,然后開始循環(huán),依次比較12個數(shù)
-
- for(i=0,k=a[0][0];i<=2;i++)
- { for(j=0;j<=3;j++)
- {
- if(a[i][j]>=k)
- {
- k=a[i][j];
- }
- }
- }
- printf("the biggest number=%d\n",k);
- //now k represents the biggest number
- //next,do again
- for(i=0;i<=2;i++)
- { for(j=0;j<=3;j++)
- {
- if(a[i][j]>=k)
- {
- printf("%d,%d\n",i,j);
- //if there are many same number,my program can solve it too
- }
- //即便有相等的情況,我們也可以輸出它的坐標(biāo)
- }
- }
- }
- //一個for循環(huán)用了三遍,我頭一次
- //第一個for是輸入元素
- //第二個是找出最大值
- //最后是輸出最大值的坐標(biāo)
- //思路清晰,程序優(yōu)良
復(fù)制代碼
|
|