题目解析
1.输入5*5的矩阵(下标从到5),包含24个0和一个1,问如何移动最小的次数(i相邻行或列)可以让1位于3行3列
举例:
输入:
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
输出:
3
注意点
1.因为数组是从0开始的,所以减2就行
row-2+col-2
2.使用整型二维数组
int number[5] [5]={0};
3.使用abs()绝对值
代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
int number[5][5]={0};
int i,j;
int row=0,col=0,count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&number[i][j]);
if(number[i][j]==1)
{
row=i;
col=j;
}
}
}
count=abs(row-2)+abs(col-2);
printf("%d",count);
system("pause");
return 0;
}