实验一(二分查找,字典序比较,矩阵转置)

 二分查找

给定⻓度为 n 且⽆重复元素的有序数组 nums ,以及⽬标值 t 。在 nums 中找到 t ,并返
回其索引(从 0 开始)。如果 t 不存在于 nums 中,返回它将会被按顺序插⼊的位置。算法
时间复杂度应为 O(logn)。
#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int>nums;//使用stl库,动态数组3
int target;
void find(int t,int l,int r)//二分查找,t为目标数,l为区间左界,r为区间右界
{
	while (l < r)//l永远小于r
	{
		int mid = (l + r) >> 1;
    	if (nums[mid]>=t) r = mid;//如果nums[mid]比目标值大,收缩右区间
    	else l = mid + 1;//反之收缩左区间
	}
	cout << r;
}
int main()
{
	cin >> n;//数组元素个数
	int tem;
	for (int i = 0; i < n; i++)
	{
		cin >> tem;
		nums.push_back(tem);//向数组中压入数据
	}
	cin >> target;//查找目标
	find(target, 0, n - 1);
	return 0;
}
//以下为几组样例
// 22 33 44 55 66
// 23 50 222 557 654 

 字典序

给定两个整数数组 a 和 b,⻓度分别为 n 和 m。如果 0 ≤ i < j, a i = b i,存在 j 使得 aj
<bj,则称 a < b。或者,对任意 i (0 ≤ i < n ), 有 a i = b i,且 n < m,则称 a <b。对任意 i (0
≤ i < n ), 有 a i = b i,且 n = m,则称 a=b 。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int>x;
	vector<int>y;
	cout << "请输入第一个数组" << endl;
	int temp;
	int i = 0;
	for (i = 0;; i++) {
		cin >> temp;
		x.push_back(temp);
		if (cin.get() == '\n')
			break;
	}
		
	cout << "请输入第二个数组" << endl;
	for (i = 0;; i++) {
		cin >> temp;
		y.push_back(temp);
		if (cin.get() == '\n')
			break;
	}
	int flag = 0;
	//flag=1表示x>y
	//flag=2表示x<y
	//flag=0表示x=y
	int a = x.size(), b = y.size();
	if (a == b)//当x的长度等于y的长度时
	{
		for (int i = 0; i < a; i++)
		{
			if (x[i] > y[i])//如果x这一位比y这一位大,那么x>y
			{
				flag = 1;
				break;
			}
			else if (x[i] < y[i])//如果x这一位比y这一位小,那么x<y
			{
				flag = 2;
				break;
			}
		}
	}
	else if (a < b)//当x的长度小于y的长度时
	{
		flag = 2;//x长度小于y,先默认x<y
		for (int i = 0; i < a; i++)
		{
			if (x[i] < y[i])//如果x这一位比y这一位小,那么x<y
			{
				flag = 2;
				break;
			}
		}
	}
	else if (a > b)//当x的长度大于y的长度时
	{
		flag = 1;//x长度大于y,先默认x>y
		for (int i = 0; i < b; i++)
		{
			if (x[i] < y[i])//如果x这一位比y这一位小,那么x<y
			{
				flag = 2;
				break;
			}

		}
	}
	if (flag == 0)
	{
		cout << "x = y" << endl;
	}
	else if (flag == 1)
	{
		cout << "x > y" << endl;
	}
	else if (flag == 2)
	{
		cout << "x < y" << endl;
	}

	return 0;
}
//123 111
//aaaa aaa
//aaa aaaa
//aa aa

矩阵转置(有点偷懒)

矩阵转置,重写 FastTranspose (程序 2.11),要求只⽤⼀个数组保存 RowSize 和 RowStart 的信息。时间复杂度 O(terms+cols)。
0a9eb8d4af2c4a83a95a66e76248bba6.png

 

#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;
class MatrixTerm {
public:
	int row, col, value;
};
class SparseMatrix
{ // a set of <row, column, value>, where row, column are 
// non-negative integers and form a unique combination;
// value is also an integer.
public:
	SparseMatrix(int r, int c, int t)
	{
		Rows = r; Cols = c; Terms = t;
		
		MatrixTerm *temp = new MatrixTerm[Terms];
		
		temp[0].row = 0;
		temp[0].col = 0;
		temp[0].value = 15;

		temp[1].row = 0;
		temp[1].col = 3;
		temp[1].value = 22;

		temp[2].row = 0;
		temp[2].col = 5;
		temp[2].value = -15;

		temp[3].row = 1;
		temp[3].col = 1;
		temp[3].value = 11;

		temp[4].row = 1;
		temp[4].col = 2;
		temp[4].value = 3;

		temp[5].row = 2;
		temp[5].col = 3;
		temp[5].value = -6;

		temp[6].row = 4;
		temp[6].col = 0;
		temp[6].value = 91;

		temp[7].row = 5;
		temp[7].col = 2;
		temp[7].value = 28;
		delete[]smArray;
		smArray = temp;
	}
	
	SparseMatrix FastTranspos()
	{
		//int* rowSize = new int[Cols]; //列元素数数组
		
		SparseMatrix B(Cols,Rows,Terms);
		//B.Rows = Cols; B.Cols = Rows;
		//B.Terms = Terms;
		if (Terms > 0) {
			int i, j;
			int* rowStart = new int[Cols]; //转置位置数组
			for (i = 0; i < Cols; i++) rowStart[i] = 0;
			for (i = 0; i < Terms; i++)
				rowStart[smArray[i].col]++;
			int temp1 = 0, temp2;
			for (int i = 1; i < Cols; i++)
			{
				temp2 = rowStart[i];
				rowStart[i] = rowStart[i - 1] + temp1;
				temp1 = temp2;
			}
			rowStart[0] = 0;
			for (i = 0; i < Terms; i++) {
				j = rowStart[smArray[i].col];
				B.smArray[j].row = smArray[i].col;
				B.smArray[j].col = smArray[i].row;
				B.smArray[j].value = smArray[i].value;
				rowStart[smArray[i].col]++;
			}// 
			delete[] rowStart;
		}
		return B;
	}//
	// creates a rc SparseMatrix with a capacity of t nonzero 
	// terms 
	void print()
	{
		cout << "Rows" << setw(8) << "Cols" << setw(8) << "value" << endl;
		for (int i = 0; i < Terms; i++)
		{
			cout << smArray[i].row << setw(8) << smArray[i].col << setw(8) << smArray[i].value<<endl;
		}
	}
//private:
	int Rows, Cols, Terms;
	MatrixTerm* smArray;
};

int main()
{
	SparseMatrix a(6, 6, 8);
	cout << "输出转置前三元组:" << endl;
	a.print();
	cout << "输出转置后三元组:" << endl;
	a.FastTranspos().print();
	return 0;
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItsNorth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值