栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构算法分析C++习题7.42对分数排序(要求使用线性算法-基数排序)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构算法分析C++习题7.42对分数排序(要求使用线性算法-基数排序)

const int N = 1000;

struct Frac
{
	int p;
	int q;
	int c;
	Frac(int p1, int q1): p(p1), q(q1)
	{
		c = int(double(p) / q * (N + 1) * (N + 1));
	}
};

void radixSort(vector& a, int n)
{
	vector> buckets(n);
	for (int i = 0; i < 2; i++)
	{
		for (int i = 0; i < a.size(); i++)
		{
			buckets[a[i].c % n ].push_back(std::move(a[i]));
		}
		int idx = 0;
		for (auto& thisBucket : buckets)
		{
			for (auto& x : thisBucket)
			{
				x.c = x.c / n;
				a[idx++] = std::move(x);

			}
			thisBucket.clear();
		}
	}
	
}




int main()
{
	vector frac;
	int numFrac;
	cout << "how many fractions: ";
	cin >> numFrac;
	int mole, deno;
	for (int i = 0; i < numFrac; i++)
	{
		cout << "enter the " << i << "th fractions:n molecule: ";
		cin >> mole;
		cout << "denominator: ";
		cin >> deno;
		frac.push_back(Frac(mole, deno));
	}
	radixSort(frac, N + 1);
	cout << "nSorted fractions:n";
	for (const auto& x : frac)
	{
		cout << x.p << "/" << x.q << endl;
	}
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号