用C++解决VOTE问题

来源:百度知道 编辑:UC知道 时间:2024/05/13 15:45:27
这是我的源程序
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
char name[10000][30];
int count[10000];
int i,j,k,h;
for(i=0;fin>>name[i];)
{
if(!i)
{
for(j=0;j<i;j++)
if(strcmp(name[i],name[j])){count[j]+=1;break;}
else{count[i]+=1;i+=1;}
}
}
for(j=0;j<i-1;)
{
h=j;
for(k=j+1;k<i;k++)
{
if(count[j]<count[k]){j=k;break;}
}
if(h=j)break;
}
for(j=h;j<i-1;j++)
{
if(count[h]=count[j]){fout<<name[j]<<endl;}
}
fout<<count[h]<<endl;

return 0;
}
不知道为什么在运行的时候生成的.EXE文件不能运行。高手帮帮忙!
#include <iostream>
#include <string>
#include <fstream>
using namespace s

for(i=0;fin>>name[i];) //i=0,没有自增语句?
{
if(!i) //i=0才执行下面语句
{
for(j=0;j<i;j++)//这个for循环的作用域是哪儿?由于i=0才执行这里语句,j<i这条件,有意义?
{
if(strcmp(name[i],name[j]))
{
count[j]+=1;break;//跳出当前循环
}
}
else{count[i]+=1;i+=1;}//不会执行else语句
}
}
for(j=0;j<i-1;) //i=0,j<i-1?...这个循环不会执行
{
h=j;
for(k=j+1;k<i;k++)
{
if(count[j]<count[k]){j=k;break;}
}
if(h=j)break;
}
for(j=h;j<i-1;j++) //h没赋值,i=0,也不会执行
{
if(count[h]=count[j]){fout<<name[j]<<endl;}
}
fout<<count[h]<<endl; //h没赋值,count[h]不明确

把第一个循环中的if(!i)改成if(i),应该就可以了

p.s:
for(j=0;j<i-1;)
{
h=j;
for(k=j+1;k<i;k++)//为什么k<i?i并不等于人数
{
if(count[j]<count[k]){j=k;break;}
}
if(h=j)break;
}
for(j=h;j<i-1;j++)
{
if(count[h]=count[j]){fout<<name[j]&