在一个n*n的矩阵中从上到下从左往右都是升序,给定一个整数x是否在矩阵中
#include <iostream>using namespace std;
int a[10][10];
void suanfa(int tr,int tc,int n,int size)//tr行 tc列
{
int s;//
if(size==1&&a[tr][tc]==n)
{
cout<<tr<<" "<<tc<<endl;
return;
}
else
return;
s=size/2;
cout<<tr<<" "<<tc<<" "<<n<<" "<<s<<endl;
if(n>=a[tr][tc]&&n<=a[tr+s][tc+s]){//在左上角
cout<<tr<<" "<<tc<<" "<<n<<" "<<s<<endl;
suanfa(tr,tc,n,s);
}
else
return;
if(n>=a[tr][tc+s]&&n<=a[tr+s][tc+size]){//在右上角
cout<<tr<<" "<<tc<<" "<<n<<" "<<s<<endl;
suanfa(tr,tc+s,n,s);
}
else
return;
if(n>=a[tr+s][tc]&&n<=a[tr+size][tc+s]){//在左下角
cout<<tr<<" "<<tc<<" "<<n<<" "<<s<<endl;
suanfa(tr,tc+s,n,s);
}
else
return;
if(n>=a[tr+s][tc+s]&&n<=a[tr+size][tc+size]){//在右下角
cout<<tr<<" "<<tc<<" "<<n<<" "<<s<<endl;
suanfa(tr,tc+s,n,s);
}
else
return;
}
int main()
{
int n,size;
cin>>n;
cin>>size;
for(int i=1;i<=size;i++)
for(int j=1;j<=size;j++)
cin>>a[i][j];
suanfai(1,1,n,size);
//cout << "Hello world!" << endl;
return 0;
}