求取若干数据的最大公约数,程序出了点问题,请高手指点

九个太阳2023 2008-02-26 10:43:13
该程序就是对"abc.txt"里的数据的最大公约数进行排序,然后输出,附"abc.txt"文件:
12 35
77 91
123 789
24 28
64 112
1024 888
98 54

************************************************下面是我的程序
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

typedef vector <int> VI;
typedef vector <VI> VVI;

int DivisorNumber(int x,int y);
void InputData(VVI&);
void print(VI);
vector <int> Fun(vector <VI> & a);

int main()
{
VVI matrix;
InputData(matrix);

VI Vec;
Vec = Fun(matrix);
sort(Vec.begin(),Vec.end());
print(Vec);

return 0;
}

//求取两个整数的最大公约数
int DivisorNumber(int x,int y)
{
//辗转相除法
int m,n,t;
if (x > y)
{
m = x;
n = y;
}
else
{
m = y;
n = x;
}

while (m % n != 0)
{
t = n;
n = m%n;
m = t;
}

return n;
}

void InputData(VVI&m)
{
ifstream in("abc.txt");
int t;
m.resize(7);
for (string s;getline(in,s);)
{
for (istringstream sin(s);sin> > t;m[m.size()-8].push_back(t));
}
}

vector <int> Fun(vector <VI> & a)
{
vector <int> c(7);

for (int i = 0; i < 7; ++i)
{
c[i] = DivisorNumber(a[i][0],a[i][1]);
}

return c;
}

void print(VI a)
{
for(int i = 0; i < 7;++i)
{
cout<<a[i];
}
}

编译的时候通过,运行的时候,出现一个严重的错误,程序不能运行,请高手指点
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
0黄瓜0 2008-02-26
  • 打赏
  • 举报
回复
第一,求公约数中交换两个数多余,象gcd就可以了.
其次,对vector的使用错误.m[m.size()-8].push_back(t)使用了一个负数下标.
m.resize(7)的使用也有问题,隐约记得resize是不分配内存的.应使用 m.reserve(7);再使用resize,然后才可以使用下标访问.最好是使用push_back.

修改后可运行的程序

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;

int DivisorNumber(int x,int y);
void InputData(VVI&);
void print(VI);
vector <int> Fun(vector <VI> & a);
int gcd(int a,int b);

int main()
{
VVI matrix;
InputData(matrix);

VI Vec;
Vec = Fun(matrix);
//sort(Vec.begin(),Vec.end());
print(Vec);

return 0;
}

//求取两个整数的最大公约数
int DivisorNumber(int x,int y)
{
//辗转相除法
int m,n,t;
if(x > y)
{
m = x;
n = y;
}
else
{
m = y;
n = x;
}

while(m % n != 0)
{
t = n;
n = m%n;
m = t;
}

return n;
}

int gcd(int a,int b)
{
int t=0;
while ( b!=0 )
{
t= a % b;
a = b;
b = t;
}
return a;
}

void InputData(VVI& m)
{
ifstream in("abc.txt");
int t;
//m.resize(7);


VI temp;
for(string s;getline(in,s);)
{

/* sin >> t;
temp.push_back(t);
sin >> t;
temp.push_back(t);
m.push_back(temp);*/
for(istringstream sin(s);sin >> t;)
{
temp.push_back(t);
}

m.push_back(temp);
temp.resize(0);
}
}

vector<int> Fun(vector<VI> & a)
{
vector<int> c(7);

for(int i = 0; i < 7; ++i)
{
int a1=a[i][0];
int a2=a[i][1];
c[i] = gcd(a[i][0],a[i][1]);
}

return c;
}

void print(VI a)
{
for(int i = 0; i < 7;++i)
{
cout <<a[i]<<",";
}
}


0黄瓜0 2008-02-26
  • 打赏
  • 举报
回复
for (istringstream sin(s);sin> > t;m[m.size()-8].push_back(t));
0黄瓜0 2008-02-26
  • 打赏
  • 举报
回复
rhf
九个太阳2023 2008-02-26
  • 打赏
  • 举报
回复
谢谢楼主,不过你这个程序没有对最大公约数排序,在main()函数里把sort()函数执行下就行了
呵呵
结贴了~~~

64,849

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧