C++数组练习

1、一维数组

#include <iostream>
using namespace std;

// 案例一:一维数组(10个案例)
int main()
{
	//1、使用一维数组存放5个学号(直接赋值)
	int arr_day01[5] = {2120180331, 2120180332, 2120180333, 2120180334, 2120180335};
	for(int i=0;i<5;i++)
	{
		cout << "第" << (i+1) << "学号: " << arr_day01[i] << endl;
	}
	
	//2、使用一维数组存放3个姓名
	string arr_day02[3] = {"猜猜一", "猜猜二", "猜猜三"};
	for(int i=0;i<3;i++)
	{
		cout << "姓名" << (i+1) << ": " << arr_day02[i] << endl;
	}
	
	//3、使用一维数组存放2个电影名称
	string arr_day03[2] = {"007", "天龙八部"};
	for(int i=0;i<2;i++)
	{
		cout << "电影" << (i+1) << ": " << arr_day03[i] << endl;
	}
	
	//4、使用一维数组存放5个班级名称
	string arr_day04[5] = {"计应一班", "计应二班", "计应三班", "计应四班", "计应五班"};
	for(int i=0;i<5;i++)
	{
		cout << "第" << (i+1) << "班级: " << arr_day04[i] << endl;
	}
	
	//5、使用一维数组存放5个 int 类型数据
	int arr_day05[5] = {111, 222, 333, 444, 555};
	for(int i=0;i<5;i++)
	{
		cout << "int数据类型:" << arr_day05[i] << endl;
	}
	
	//6、使用一维数组存放5个 string 类型数据
	string arr_day06[5] = {"test01", "test02", "test03", "test04", "test05"};
	for(int i=0;i<5;i++)
	{
		cout << "string类型:" << arr_day06[i] << endl;
	}
	
	//7、使用一维数组存放5个 double 类型数据
	double arr_day07[5] = {111.11, 222.22, 333.33, 444.44, 555.55};
	for(int i=0;i<5;i++)
	{
		cout << "double数据类型:" << arr_day07[i] << endl;
	}
	
	//8、使用一维数组存放5个年龄
	long arr_day08[5] = {18, 22, 23, 24, 25};
	for(int i=0;i<5;i++)
	{
		cout << "年龄:" << arr_day08[i] << endl;
	}
	
	//9、使用一维数组存放5个宿舍号
	long arr_day09[5] = {3442, 3443, 3444, 3445, 3446};
	for(int i=0;i<5;i++)
	{
		cout << "年龄:" << arr_day09[i] << endl;
	}
	
	//10、使用一维数组存放5地址名
	string arr_day010[5] = {"城大一", "城大二", "城大三", "城大四", "城大五"};
	for(int i=0;i<5;i++)
	{
		cout << "地址:" << arr_day010[i] << endl;
	}
	return 0;
}


2、二维数组

#include <iostream>
using namespace std;

// 案例一:二维数组(10个案例)
int main()
{
	//1、n个人的姓名学号
	string str_day01[3][2] = {{"猜猜一", "2120180332"}, {"猜猜二", "2120180333"}, {"猜猜三", "2120180334"}};
	for(int i=0;i<3;i++)
	{
		string str = "";
		for(int j=0;j<2;j++)
		{
			str += str_day01[i][j];
		}
		cout << str << endl;
	}
	
	//2、2个宿舍所有人姓名
	string str_day02[2][6] = 
	{
		{"猜猜一", "猜猜二", "猜猜三", "猜猜四", "猜猜五", "猜猜六"}, 
		{"ruanjia1", "ruanjia2", "ruanjia3", "ruanjia4", "ruanjia5", "ruanjia6"}
	};
	for(int i=0;i<2;i++)
	{
		cout << "第" << (i+1) << "个宿舍所有人姓名:" << endl;
		for(int j=0;j<6;j++)
		{
			cout << str_day02[i][j] << endl;
		}
	}
	
	/*
	3、公司年销售额求和
		某公司按照季度和月份统计的数据如下:单位(万元)
		第一季度:22,66,44
		第二季度:77,33,88
		第三季度:25,45,65
		第四季度:11,66,99
	*/
	double arr_day03[4][3] = {{22,66,44}, {77,33,88}, {25,45,65}, {11,66,99}};
	double sum = 0.00;
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<3;j++)
		{
			sum += arr_day03[i][j];
		}
	}
	cout << "年销售额求和= " << sum << "万元" << endl;
	
	/*
	4、使用二维数组保存4个班级的学生身高(求每个班级最高身高,最低身高,平均身高)
	*/
	double str_day04[4][4] = 
	{
		{167.7, 170.8, 177, 169.8}, 
		{187.7, 160.8, 157, 189.8}, 
		{164.7, 173.8, 137, 149.8}, 
		{163.7, 172.8, 177, 161.8}
	};
	double x,max,min,avg;
	for(int i=0;i<4;i++)
	{
		cout << "第" << (i+1) << "个班级" << endl;
		max = str_day04[i][0];
		min = str_day04[i][0];
		x = 0.00;
		for(int j=0;j<4;j++)
		{
			x += str_day04[i][j];
			if(str_day04[i][j] > max)
			{
				max = str_day04[i][j];
			} 
			if(str_day04[i][j] < min)
			{
				min = str_day04[i][j];
			}
		}
		cout << "最高身高:" << max << endl;
		cout << "最低身高:" << min << endl;
		cout << "平均身高:" << (x/4) << endl;
	}

	/*
	5、使用二维数组保存n个班级的学生成绩(求每个班级最高成绩,最低成绩,平均成绩)
	*/
	double arr_day05[3][50];
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<50;j++)
		{
			arr_day05[i][j] = rand() % 100 + 1;
		}
	}
	for(int i=0;i<3;i++)
	{
		double mx = arr_day05[i][0], mi = arr_day05[i][0], ag = 0.00;
		cout << "第" << (i+1) << "个班级" << endl;
		for(int x=0;x<50;x++)
		{
			if(mx < arr_day05[i][x])
			{
				mx = arr_day05[i][x];
			}
			if(mi > arr_day05[i][x])
			{
				mi = arr_day05[i][x];
			}
			ag += arr_day05[i][x];
		}
		cout << "最高成绩:" << mx << endl;
		cout << "最低成绩:" << mi << endl;
		cout << "平均成绩:" << (ag/50) << endl;
	}
	
	// 6、找出5个电影院的3个热门电影
	string str_day06[5][3] = {
		{"001", "002", "003"},
		{"101", "102", "103"},
		{"201", "202", "203"},
		{"301", "302", "303"},
		{"401", "402", "403"}
	};
	for(int i=0;i<5;i++)
	{
		cout << "第" << (i+1) << "个电影院" << endl;
		for(int j=0;j<3;j++)
		{
			cout << str_day06[i][j] << endl;
		}
	}
	
	// 7、二维数组存储多个int类型一个数组(一维数组长度为5),对每个一维数组数据进行排序输出(降序:高 -> 低);
	int arr07[3][5] = {
		{32,5,88,22,44},
		{1,5,45,422,65},
		{89,76,43,33,32}
	};
	for(int i=0;i<3;i++)
	{
		// 比较次数:n-1
		for(int j=0;j< (5-1);j++)
		{
			bool isSort  = true;
			for(int x=0;x<((5-1) - j);x++)
			{
				if(arr07[i][x] < arr07[i][x+1])
				{
					isSort = false;
					int temp = arr07[i][x];
					arr07[i][x] = arr07[i][x+1];
					arr07[i][x+1] = temp;
				}
			}
			if(isSort) break;
		}
	}
	for(int i=0;i<3;i++)
	{
		cout << "第" << (i+1) << "个数组排序结果:";
		for(int j=0;j<5;j++)
		{
			cout << arr07[i][j] << " ";
		}
		cout << endl;
	}
	
	//8、插入排序
	int arr08[3][5] = 
	{
		{32,5,88,22,44},
		{1,5,45,422,65},
		{89,76,43,33,32}
	};
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<(5-1);j++)
		{
			int end = j;
			int tem = arr08[i][j+1];
			while(end >= 0)
			{
				if(tem > arr08[i][end])
				{
					arr08[i][end+1] = arr08[i][end];
					end--;
				}
				else
				{
					break;
				}
			}
			arr08[i][end+1] = tem;
		}
	}
	
	for(int i=0;i<3;i++)
	{
		cout << "第" << (i+1) << "个数组排序结果:";
		for(int j=0;j<5;j++)
		{
			cout << arr08[i][j] << " ";
		}
		cout << endl;
	}
	
	//9、使用二维数组存储n个宿舍所有人姓名
	string arr09[2][6] = {
		{"佳佳一", "佳佳二", "佳佳三", "佳佳四", "佳佳五", "佳佳六",},
		{"张三一", "张三二", "张三三", "张三四", "张三五", "张三六",}
	};
	for(int i=0;i<2;i++)
	{
		cout << "第" << (i+1) << "个宿舍:" << endl;
		for(int j=0;j<6;j++)
		{
			cout << arr09[i][j] << endl;
		}
	}
	
	//10、使用二维数组存储3个公司部门名称(万元)
	string arr10[2][2] = {
		{"销售部", "运维部"},
		{"财务部", "开发部"}
	};
	for(int i=0;i<2;i++)
	{
		cout << "第" << (i+1) << "个公司部门: " << endl;
		for(int j=0;j<2;j++)
		{
			cout << arr10[i][j] << endl;
		}
	}
	
	return 0;
}

3、三维数组

#include <iostream>
using namespace std;

//三维数组使用案例(10个)
int main()
{
	cout << "1、使用三维数组存储3个公司的3部门的员工名" << endl;
	string arr01[3][3][2] = 
	{
		{
			{"张三", "李四"},
			{"小明", "王五"},
			{"小红", "小紫"}
		},
		{
			{"狗狗", "狗子"},
			{"猫猫", "猫子"},
			{"鸭鸭", "鸭子"}
		},
		{
			{"佳佳", "珈珈"},
			{"家家", "甲甲"},
			{"加加", "假假"}
		}
	};
	for(int i=0;i<3;i++)
	{
		cout << "第" << (i+1) << "公司:" << endl;
		for(int j=0;j<3;j++)
		{
			cout << "第" << (j+1) << "部门员工姓名:" <<  endl;
			for(int x=0;x<2;x++)
			{
				cout << arr01[i][j][x] << endl;
			}
		}
	}
	
	cout << "2、使用三维数组存储3个班级的学号成绩" << endl;
	int arr02[3][2][2] = {
		{
			{2120180332, 2120180332},
			{33,44}
		},
		{
			{2120180333, 2120180334},
			{53,54}
		},
		{
			{2120180336, 2120180335},
			{63,67}
		}
	};
	for(int i=0;i<3;i++)
	{
		cout << "第" << (i+1) << "班级" << endl;
		for(int j=0;j<2;j++)
		{
			if(j == 0)
			{
				cout << "学号:" << endl;
			} else
			{
				cout << "成绩:" << endl;	
			}
			for(int x=0;x<2;x++)
			{
				cout << arr02[i][j][x] << endl;
			}
		}
	}
	
	cout << "3 、使用三维数组存储5个int类型数组后排序(冒泡)" << endl;
	int arr03[2][3][5] = {
		{
			{12, 44, 1, 55, 38},
			{22, 54, 7, 5, 78},
			{32, 64, 8, 5, 88}
		},
		{
			{54, 74, 31, 65, 38},
			{132, 34, 27, 55, 28},
			{52, 14, 18, 45, 8}
		}
	};
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<3;j++)
		{
			for(int x=0;x<(5-1);x++)
			{
				for(int y=0;y<((5-1)-x);y++)
				{
					if(arr03[i][j][y] > arr03[i][j][y+1])
					{
						int temp = arr03[i][j][y+1];
						arr03[i][j][y+1] = arr03[i][j][y];
						arr03[i][j][y] = temp;
					}
				}
			}
		}
	}
	for(int i=0;i<2;i++)
	{
		cout << "第" << (i+1) << "个数组:" << endl;
		for(int j=0;j<3;j++)
		{
			cout << "第" << (j+1) << "排序结果:" << endl;
			for(int x=0;x<5;x++)
			{
				cout << arr03[i][j][x] << " ";
			}
			cout << endl;
		}
	}
	
	cout << "4、使用三维数组存储5个int类型数组后排序(插入排序)" << endl;
	int arr04[2][3][5] = {
		{
			{12, 44, 1, 55, 38},
			{22, 54, 7, 5, 78},
			{32, 64, 8, 5, 88}
		},
		{
			{54, 74, 31, 65, 38},
			{132, 34, 27, 55, 28},
			{52, 14, 18, 45, 8}
		}
	};
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<3;j++)
		{
			for(int x=0;x<(5-1);x++)
			{
				int end = x;
				int tem = arr04[i][j][x+1];
				while(end >= 0)
				{
					if(tem > arr04[i][j][end])
					{
						arr04[i][j][end+1] = arr04[i][j][end];
						end--;
					}		
					else
					{
						break;
					}
				}
				arr04[i][j][end+1] = tem;
			}
		}
	}
	for(int i=0;i<2;i++)
	{
		cout << "第" << (i+1) << "个数组:" << endl;
		for(int j=0;j<3;j++)
		{
			cout << "第" << (j+1) << "排序结果:" << endl;
			for(int x=0;x<5;x++)
			{
				cout << arr04[i][j][x] << " ";
			}
			cout << endl;
		}
	}
	
	
	cout << "5、使用三维数组存储n个宿舍学生学号、成绩" << endl;
	int arr05[3][2][2] = 
	{
		{
			{2120180332, 2120180333},
			{88, 99}
		},
		{
			{2120180334, 2120180335},
			{77, 89}
		},
		{
			{2120180345, 2120180345},
			{68, 69}
		}
	};
	for(int i=0;i<3;i++)
	{
		cout << "第" << (i+1) << "个宿舍:" << endl;
		for(int j=0;j<2;j++)
		{
			if(j == 0)
			{
				cout << "学号:" ;
			} else
			{
				cout << "成绩:" ;	
			}
			for(int y=0;y<2;y++)
			{
				cout << arr05[i][j][y] << " ";
			}
			cout << endl;
		}
	}
	
	cout << "6、某公司所有部门员工工资、员工工号" << endl;
	double arr06[3][2][5] =
	{
		{
			{8888.88, 4345.12, 7563.11, 9808.55, 4345.22},
			{1111, 2222, 3333, 4444, 5555}
		},
		{
			{8888.88, 4345.12, 7563.11, 9808.55, 4345.22},
			{1211, 1222, 1233, 1244, 1255}
		},
		{
			{8888.88, 4345.12, 7563.11, 9808.55, 4345.22},
			{2211, 3322, 4433, 5544, 6655}
		}
	};
	for(int i=0;i<3;i++)
	{
		cout << "第" << (i+1) << "个部门:" << endl;
		for(int j=0;j<2;j++)
		{
			for(int ii=0;ii<5;ii++)
			{
				cout << arr06[i][j][ii] << " ";
			}
			cout << endl;
		}
	}
	
	cout << "7、2个班级4个宿舍人的姓名,学号" << endl;
	string arr07[2][4][2][2] = 
	{
		{
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			},
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			},
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			},
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			}
		},
		{
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			},
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			},
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			},
			{
				{"test01", "test01"},
				{"2120180332", "2120180332"}
			}
		}
	};
	for(int i=0;i<2;i++)
	{
		cout << "第" << (i+1) << "个班级:" << endl;
		for(int j=0;j<4;j++)
		{
			cout << "第" << (j+1) << "个宿舍:" << endl;
			for(int x=0;x<2;x++)
			{
				for(int y=0;y<2;y++)
				{
					cout << arr07[i][j][x][y] << " ";
				}
				cout << endl;
			}
		}
	}
		
	return 0;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值