从文件中读取分别一个一个数据,并查集的联系读取64行到67行

来源:互联网  时间:2016/8/26 21:33:33

关于网友提出的“ 从文件中读取分别一个一个数据,并查集的联系读取64行到67行”问题疑问,本网通过在网上对“ 从文件中读取分别一个一个数据,并查集的联系读取64行到67行”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 从文件中读取分别一个一个数据,并查集的联系读取64行到67行
描述:

本帖最后由 qq_30560635 于 2015-11-17 10:28:33 编辑

算法数据iostreamc语言

#include
#include  
#include 
#include
#include  
#include  
#include  
#include 
using namespace std;
int  pre[1300],j;
int  rank[1300]{0};
bool t[1300];                             //t 用于标记独立块的根结点
int Find(int x)
{
int r = x;
while (r != pre[r])
r = pre[r];                    //寻找到指定的老大pre[r]=r.
int i = x, j;
while (pre[i] != r) //r已经是老大的位置了
{
j = pre[i];   //记录pre[i]的上级是谁
pre[i] = r;      //路径压缩 //更改pre[i]的上级直接为boss
i = j; //找到pre[i]的上级,以便找到上级的上级以便最后寻boss,且把每个上级的上级弄成boss
}
return r;
}
void mix(int x, int y)                                                         //                 按秩                           void mix(int x,int y)
{                                                                                        //                                                     {
int fx = Find(x), fy = Find(y);                          //找两个人的老大 int fx=Find(x),fy=Find(y)                                                                         //                                                         if(fx==fy)  return;
if (fx != fy) {           //   if(rank[x]>rank[y])
              //           pre[fy]=fx;  把小根合到大根上
     //大根合小根,小根合大根不知道        else{
pre[fy] = fx;             // 老大是fy                     pre[fx]=fy;
j--;            //一个人的老大弄成另一个的老大     if(rank[x]==rank[y])                                           // 多一个子,加枝叶   rank[y]++;
}  // }                                                                                                                               //                  }
}
                                                              
int main()
{
int N, M, a, b, i,m,n;
ifstream score;
score.open("score.dat", ios::in);
ifstream score1;
score.open("score1.dat", ios::in);
if (!score.is_open()&&!score1.is_open()) {
cout << "打开文件失败" << endl;
return -1;
}
cin>>N>>M;
j = N;
for (i = 1; i <= N; i++)          //初始化 
pre[i] = i;
for (i = 1; i <= M; i++)          //吸收并整理数据  
{
score >> a;                  //我有两个dat文件,但是读入不了,直接停止程序了
score1 >> b;               //即使只读了10个数据进去,自己一次次输入联系太累了呀
mix(a, b);           
}
cout << "现在有" << j << "个集合" << endl;
paris:
cout << "请给两个点看是否联通" << endl;
cin >> m >> n;
if (Find(m) == Find(n))
cout << "此两点联通" << endl;
else
cout << "此两点未联通" << endl;
goto paris;
score.close();
return 0;
}/

解决方案1:

score1不是什么都没做吗?你看open的地方,是原来就写错了,还是发上来错了

解决方案2:

文件路径中的\要用\\来表示:
"d:\\mydir\\a\\xxx.dat"

解决方案3:

另存为,编码选ANSI

解决方案4:

dat文件中的每个数之间必须以空格或Tab字符或回车换行间隔

上一篇poco生成ARM格式 make过程出现的错误!!!
下一篇用的VS2013
明星图片
相关文章
《 从文件中读取分别一个一个数据,并查集的联系读取64行到67行》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)