数据结构殷人昆编程练习第一章概论

该编程练习聚焦于数据结构基础知识,包括如何找出三个整数中的最大值、最小值以及中间值,比较两个整数的效率分析,以及统计数组元素在特定区间分布情况的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.15 求三个整数中的最大、最小和中间数。

#include<iostream>
using namespace std;

void fun(int a, int b, int c){
	int max, min, center;
	if (a > b&&a > c){
		max = a;
		if (b > c){min = c; center = b;}
		else{ min = b; center = c; }
	}
	if (b > a&&b > c){
		max = b;
		if (a > c){ min = c; center = a; }
		else{ min = a; center = c; }
	}
	else{
		max = c;
		if (a > b){ min = b; center = a; }
		else{ min = a; center = b; }
	}
	cout << "max: " << max << endl;
	cout << "center: " << center << endl;
	cout << "min: " << min << endl;
}

void main(){
	int x, y, z;
	cout << "please enter three int number: ";
	cin >> x >> y >> z;
	fun(x, y, z);

	system("pause");
}

1.16 比较两个整数大小,并求其时间复杂度

#include<iostream>
using namespace std;

void compare(int a, int b){
	if (a > b)
		cout << ">";
	if (a < b)
		cout << "<";
	if (a == b)
		cout << "==";
}
// 时间复杂度O(1)
void main(){
	int a, b;
	cout << "please enter two int number: ";
	cin >> a >> b;
	compare(a, b);
	system("pause");

}

1.17 统计数组中元素落在不同区间的个数

#include<iostream>
using namespace std;

void fun(int a[],int n)
{
	int cnt1=0, cnt2=0, cnt3=0, cnt4=0, cnt5=0;
	for (int i = 0; i < n; ++i)
	{
		if (0 <= a[i] && a[i] <= 20) cnt1 += 1; 
		if (20 < a[i] && a[i] <= 50) cnt2 += 1;
		if (50 < a[i] && a[i] <= 80) cnt3 += 1;
		if (80 < a[i] && a[i] <= 130) cnt4 += 1;
		if (130 < a[i] && a[i] <= 200) cnt5 += 1;
	}
	cout<< "0-20: " << cnt1 << '\n'
		<< "21-50: " << cnt2 << '\n'
		<< "51-80: " << cnt3 << '\n'
		<< "81-130: " << cnt4 << '\n'
		<< "131-200: " << cnt4 << '\n';
}

void main()
{
	const int n = 10;
	int a[n] = { 101, 23, 1, 66, 78, 35, 99, 170, 121, 55 };
	fun(a, n);
	system("pause");

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值