凤戏花丛 2015-12-17 14:46 采纳率: 0%
浏览 1239

請大神幫忙看看,到底是那裡出錯了

#include
using std::cout;
using std::cin;
using namespace std;
class chazhaobiao
{
private:
int *date;
int total;
public:
chazhaobiao(int a[],int n)//n是共有多少個元素
{
date=a;
total=n;
}
int search_date(int m)//m是要查詢的數據,要查找需要先排序
{
int after[total];
for(int i=0;i<total;i++)
after[i]=date[i];
int i, j, t, k;
for(i = 0; i < total-1; i++)
{
k = i;
for(j = i+1; j < total; j++)
{
if(after[j] < after[k])
k = j;
}
if(k != i)
{
t = after[k];
after[k] = after[i];
after[i] = t;
}
}

    int right=total,left=0,middle=(right+left)/2;
    int pp=0;
    while(pp==1||pp==-1)
    {
        if(m==after[middle])
        {
            cout<<middle;
            pp=1;
        }
        else if(m<after[middle])
            {

                right=middle;
                middle=(right+left)/2;
            }
              else {
                    left=middle;
                    middle=(right+left)/2;
                    }
        if(middle<0||middle>total)
        {
            cout<<"查詢失敗";
            pp=-1;
        }
    }
}

};

int main()
{
​ cout << "請輸入元素個數:";
int mumber;
std::cin>>mumber;
int s[mumber];
cout<<"請輸入元素:";
for(int i=0;i cin>>s[i];
chazhaobiao b1(s,mumber);
while(true)
{
cout<<"輸入要查詢的元素:";
int temp=0;
cin>>temp;
temp=b1.search_date(temp);
cout<<temp<<endl;
break;
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-12-19 02:11
    关注

    #if 1
    #include
    using namespace std;

    class chazhaobiao
    {
    private:
    int *date;
    int total;
    public:
    chazhaobiao(int a[], int n)//n是共有多少個元素
    {
    date = a;
    total = n;
    }
    int search_date(int m)//m是要查詢的數據,要查找需要先排序
    {
    //int after[total]; //total并不是常量怎么能够作为数组下标呢,并且正因为如此导致你的程序根本就运行不下去...

        for (int i = 0; i<total; i++)
            after[i] = date[i];
        int i, j, t, k;
        for (i = 0; i < total - 1; i++)
        {
            k = i;
            for (j = i + 1; j < total; j++)
            {
                if (after[j] < after[k])
                    k = j;
            }
            if (k != i)
            {
                t = after[k];
                after[k] = after[i];
                after[i] = t;
            }
        }
        int right = total, left = 0, middle = (right + left) / 2;
        int pp = 0;
        while (pp == 1 || pp == -1)
        {
            if (m == after[middle])
            {
                cout << middle;
                pp = 1;
            }
            else if (m<after[middle])
            {
    
                right = middle;
                middle = (right + left) / 2;
            }
            else {
                left = middle;
                middle = (right + left) / 2;
            }
            if (middle<0 || middle>total)
            {
                cout << "查詢失敗";
                pp = -1;
            }
        }
    }
    

    };
    int main()
    {
    ​ cout << "請輸入元素個數:";
    int mumber;
    std::cin >> mumber;
    int s[mumber];
    cout << "請輸入元素:";
    for (int i = 0; i cin >> s[i];
    chazhaobiao b1(s, mumber);
    while (true)
    {
    cout << "輸入要查詢的元素:";
    int temp = 0;
    cin >> temp;
    temp = b1.search_date(temp);
    cout << temp << endl;
    break;
    }
    return 0;
    }
    #endif

    基础知识不扎实

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀