注册 登录
编程论坛 C++ Builder

在一个n*n的矩阵中从上到下从左往右都是升序,给定一个整数x是否在矩阵中

autok 发布于 2016-12-18 12:54, 3362 次点击
#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;
}
2 回复
#2
autok2016-12-18 12:55
程序有错求解
#3
yangfrancis2017-02-11 15:15
回复 2楼 autok
suanfa函数一开始的if条件两个分支都已把函数给掐断了,后面还写那么多做甚???
1