|
|
发表于 2014-6-17 23:56:32
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct{
- int row;
- int col;
- }Pos; //定义一个存储位置的数据结构
- int FindNum(int (*arr)[100],int rows,int cols,int num,Pos *pos){
- int row,col;
- int bIsCnt=1; //是否继续寻找
- for(row=0;bIsCnt&&row<rows;row++)
- for(col=0;bIsCnt&&col<cols;col++){
- if(arr[row][col]==num){
- pos->row=row; //找到的行和列写入该数据结构
- pos->col=col;
- bIsCnt=0; //跳出循环
- }
- }
- if(bIsCnt) return 0; //没有找到返回0
- return 1; //找到返回1
- }
- int main()
- {
- int arr[100][100],rows,cols;
- int i,j,num;
- Pos *pos=(Pos *)malloc(sizeof(Pos)); //开辟一个空间存储位置数据
- printf("输入行和列(空格间开)");
- scanf("%d%d",&rows,&cols);
- printf("输入数据:\n");
- for(i=0;i<rows;i++)
- for(j=0;j<cols;j++)
- scanf("%d",&arr[i][j]); //输入数据
- printf("输入要查询的数据:");
- scanf("%d",&num);
- if(FindNum(&(arr[100]),rows,cols,num,pos)){ //返回1则找到
- printf("num %d - row:%d col:%d\n",num,pos->row,pos->col);
- }
- else{ //没有找到
- printf("Not Found!\n");
- }
- return 0;
- }
复制代码
|
|