#include
#include
#include
#include <windows.h>
#include
#include
#include
using namespace std;
//冒泡排序和选择排序
int main()
{
int temp;
int nums[] = { 8,4,2,1,23,23,344,12 };
int numslen = sizeof(nums) / sizeof(int);
//擂台的变量
int min = nums[0];//假设最小值是数组的第一个元素
int minIndex = 0;//最小值的初始下表设置为0
for (int i = 0; i < numslen; i++) {
min = nums[i];
minIndex = i;
for (int j = i + 1; j < numslen; j++) {
//打擂台
if (nums[j] < min) {
min = nums[j];
minIndex = j;
}
}
//交换
if (minIndex > i) {
temp = nums[minIndex];
nums[minIndex] = nums[i];
nums[i] = temp;
}
}
cout << “排序后:” << endl;
for (int i = 0; i < numslen; i++) {
cout << nums[i] << endl;
}
//逆序
for (int i = 0; i < numslen/2; i++) {
temp = nums[i];
nums[i] = nums[numslen - i - 1];
nums[numslen - i - 1] = temp;
}
cout << “排序后:” << endl;
for (int i = 0; i < numslen; i++) {
cout << nums[i] << endl;
}
getchar();
return 0;
}
//冒泡排序
//1.第一轮
/*int temp;
int nums[] = { 15,25,90,23,9 };
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 4 - i; j++) {
if (nums[j] < nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
cout << nums[i] << endl;
}
getchar();
return 0;
}*/
/*int Newnums[] = { 8,4,2,1,23,344,12 };
int numslen = sizeof(Newnums) / sizeof(int);//计算出数组的长度
for (int i = 0; i < numslen; i++) {
cout << Newnums[i] << endl;
}
//累加
int sum = 0;
int ava = 0;
for (int i = 0; i < numslen; i++) {
sum += Newnums[i];
}
cout << sum << endl;
ava = sum / numslen;
cout << ava << endl;
//数组的最大值和最小值 发擂台
int max = Newnums[0];
int maxIndex = 0;
for (int i = 1; i < numslen; i++) {
if (Newnums[i] > max) {
max = Newnums[i];
maxIndex = i;
}
/*else {
continue;
}*/
//}
/*cout << "最大值是:" << max << '/' << Newnums[maxIndex] << endl;
cout << "最大数值的下标:" << maxIndex << endl;
//统计奇数偶数的个数
int jCount = 0;
int oCount = 0;
for (int i = 0; i < numslen; i++) {
if (Newnums[i] % 2 == 0) {
oCount++;
}
if (Newnums[i] % 2 != 0) {
jCount++;
}
}
cout << "偶数的个数" << oCount << endl;
cout << "奇数的个数" << jCount << endl;
//查找输入数字在数组中的下标,没有找到下标为-1
int searchNum;
int searchIndex = INT_MIN;
cout << "请输入需要查找的数字:";
cin >> searchNum;
//使用循环查找
for (int i = 0; i < numslen; i++) {
if (Newnums[i] == searchNum) {
searchIndex = i;
cout << "你已经找到想要的数值啦!";
break;
}
}
if (searchIndex == INT_MIN) {
cout << "没有找到最小的数字" << endl;
getchar();
}
else {
cout << "找到了" << searchIndex+1 << endl;
getchar();
}*/
//动态从键盘录入信息并赋值
/*const int N = 5;
int nums[N];
for (int i = 0; i < N; i++) {
cout << "请输入第" << (i + 1) << "个数组元素";
cin >> nums[i];
}
for (int i = 0; i < N; i++) {
cout << nums[i] << endl;
}
getchar();
return 0;
//外层循环控制行数(行数和换行)
//内层循环控制列数(列数和图像)
/*for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2 - i; j++) {
cout << " ";
}
for (int j = 0; j <= 2 * i; j++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < i; j++) {
cout << " ";
}
for (int j = 0; j <=4- 2*i; j++) {
if (j == 0 || j == 4-2*i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}*/
int main() {
double power[99];
int powerCount = 0;
double insertPower;
int insertIndex = 0;
power[powerCount++] = 45771;
power[powerCount++] = 42322;
power[powerCount++] = 40907;
power[powerCount++] = 40706;
double temp;
for (int i = 0; i < powerCount; i++) {
for (int j = 0; j < powerCount - i - 1; j++) {
if (power[j] < power[j + 1]) {
temp = power[j + 1];
power[j] = power[j + 1];
power[j + 1] = temp;
}
}
}
cout << "排序后:" << endl;
for (int i = 0; i < powerCount; i++) {
cout << power[i] << '\t';
}
cout << endl;
cout << "请输入要插入的数字:";
cin >> insertPower;
insertIndex = powerCount ;//
//1.找到第一个比insertIndex大的数字
for (int i = 0; i < powerCount; i++) {
if (insertPower > power[i]) {
insertIndex = i;
break;
}
}
//2.从最后一个元素开始,将数字复制到后面的一个元素中
for (int i = powerCount - 1; i >= insertIndex; i--) {
power[i + 1] = power[i];
}
//3.将要插入的数据插在空留出来的位置上
power[insertIndex] = insertPower;
//4.将数组的总长度+1
powerCount++;
cout << "输出插入后的效果:"<<endl;
for (int i = 0; i < powerCount; i++) {
cout << power[i] << '\t';
getchar();
}
getchar();
//return 0;
}
int main() {
//使用二维数组
string stu_names[]{ “刘备”,“关羽”,“张飞” };
string course_names[]{ “语文”,“数学”,“英语” };
const int ROW = 3;
const int COL = 3;
double scores[ROW][COL];
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
cout << stu_names[i] << “的” << course_names[j] << “成绩:”;
cin >> scores[i][j];
}
}
//打印
cout << "\t";
for (int i = 0; i < COL; i++) {
cout << course_names[i] << '\t';
}
cout << endl;
for (int i = 0; i < ROW; i++) {
cout << stu_names[i] << '\t';
for (int j = 0; j < COL; j++) {
cout << scores[i][j] << "\t";
}
cout << endl;
getchar();
}
}