我的代码存在二分搜索问题......请查看我的以下问题?

发布于 2024-10-27 22:01:02 字数 2339 浏览 2 评论 0原文

#include<iostream>
#include<string>

using namespace std;

int main()
{
int list[25],vote[25];
bool found;
int count,first,mid,last;
char condition;
double high = 0,total=0;
string searchitem,name[25],winner;
cout <<"How many names?" <<endl;
cin>>count;
cout <<" Enter the "<<count<< " candidates last name and the number of votes received " <<endl;
for (int i=0;i<count;i++)
{
    cin >>name[i];
    cin >> vote[i];
    total = total+vote[i];

}

cout <<"Candidate names ,number of votes and their percentage of votes are ..."<<endl;
for (int i=0;i<count;i++)
{
    cout << name[i]<<endl;
    cout << vote[i]<<endl;
    cout << (vote[i]*100)/total<<endl;
    if(vote[i]>high)
    {
        high = vote[i];
     winner = name[i];
    }

}
cout << " The winner of the election is " << winner << endl;

cout << "Do you have anything more to do?y/n?" << endl;
cin >> condition;
while (condition=='y' || condition=='Y')
{
found = false;
cout<<"Enter name you want to search \n";
cin>>searchitem;
first=0;
last=count-1;
while ((first<=count)&&!found)
{
    mid=(first+last)/2;
    if (name[mid]==searchitem)
    {
        found=true;
        total =  total-vote[mid];
        cout << "Enter the new number of votes for " << name[mid] << endl;
        cin >> vote[mid];
        total = total + vote[mid];
    }
    else if (name[mid]>searchitem)
    {
        last=mid-1;
    }
    else first=mid+1;
}
if (!found)
{
    cout<<" Sorry that name was not found \n";
}

cout <<"Candidate names ,number of votes and their percentage of votes are ..."<<endl;
high = 0;
for (int i=0;i<count;i++)
{
    cout << name[i]<<endl;
    cout << vote[i]<<endl;
    cout << (vote[i]*100)/total<<endl;
    if(vote[i]>high)
    {
        high = vote[i];
     winner = name[i];
    }
}
cout << " The winner is " << winner << endl;

cout << "Do you have anything more to do?y/n? "<<endl;
cin >> condition;
}
}

好吧,现在我一直在尝试这个,当涉及到 5 个名字时,我能够改变第三个和第四个名字的投票并显示结果。 但是当我尝试搜索第一个、第二个和姓氏时,我的程序就停止了! 为什么会发生这种情况???并且解决方案可以帮助我节省尝试徒手打破墙壁的时间。

#include<iostream>
#include<string>

using namespace std;

int main()
{
int list[25],vote[25];
bool found;
int count,first,mid,last;
char condition;
double high = 0,total=0;
string searchitem,name[25],winner;
cout <<"How many names?" <<endl;
cin>>count;
cout <<" Enter the "<<count<< " candidates last name and the number of votes received " <<endl;
for (int i=0;i<count;i++)
{
    cin >>name[i];
    cin >> vote[i];
    total = total+vote[i];

}

cout <<"Candidate names ,number of votes and their percentage of votes are ..."<<endl;
for (int i=0;i<count;i++)
{
    cout << name[i]<<endl;
    cout << vote[i]<<endl;
    cout << (vote[i]*100)/total<<endl;
    if(vote[i]>high)
    {
        high = vote[i];
     winner = name[i];
    }

}
cout << " The winner of the election is " << winner << endl;

cout << "Do you have anything more to do?y/n?" << endl;
cin >> condition;
while (condition=='y' || condition=='Y')
{
found = false;
cout<<"Enter name you want to search \n";
cin>>searchitem;
first=0;
last=count-1;
while ((first<=count)&&!found)
{
    mid=(first+last)/2;
    if (name[mid]==searchitem)
    {
        found=true;
        total =  total-vote[mid];
        cout << "Enter the new number of votes for " << name[mid] << endl;
        cin >> vote[mid];
        total = total + vote[mid];
    }
    else if (name[mid]>searchitem)
    {
        last=mid-1;
    }
    else first=mid+1;
}
if (!found)
{
    cout<<" Sorry that name was not found \n";
}

cout <<"Candidate names ,number of votes and their percentage of votes are ..."<<endl;
high = 0;
for (int i=0;i<count;i++)
{
    cout << name[i]<<endl;
    cout << vote[i]<<endl;
    cout << (vote[i]*100)/total<<endl;
    if(vote[i]>high)
    {
        high = vote[i];
     winner = name[i];
    }
}
cout << " The winner is " << winner << endl;

cout << "Do you have anything more to do?y/n? "<<endl;
cin >> condition;
}
}

Alright.Now I have been trying to experiment with this and when there are 5 names involved, Im able to alter the third and the fourth name's votes and display the results.
But when I try searching for the first,second and the last name my program just halts!
Why is this happening???And a solution can help save me time from trying to break a wall with bare hands.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-11-03 22:01:02
while ((first<=count)&&!found)

应该是

while ((first<=last)&&!found)
while ((first<=count)&&!found)

should be

while ((first<=last)&&!found)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文