#include<iostream>
using namespace std;
void my_sort(int arr[], int n);//冒泡排序
int main()
{
int size ;
cout << "please put in the size :";
cin >> size;
const int MAX = 10000;
//这里用new开辟一块新的内存单元 后面记得用delete[]arr;销毁
int* arr = new int[size];//下面要对arr输入元素,所以说不用进行输出化
int* count = new int[MAX]();//这里这个格式后面的()代表了对count进行初始化,int类型默认的初始化为0
for (int i = 0; i < size; i++)
{
cin >> arr[i];//进行元素输入
count[arr[i]+MAX/2]++;//这里很好,用里一个数组来记录每个元素出现的次数,进行自加
//因为这里考虑了负数所以进行了负数偏移,就是负数没法作为数组的下标,所以先加上一个数来代替
//这个负数存储出现了几次,每出现一次就进行一次++
}
my_sort(arr, size);
cout << "N Count" << endl;
for (int i = 0; i < size; i++)
{
if (arr[i] == arr[i + 1])//这里写得好
continue;//这里是用来当出现了多个重复元素,只输出一遍(只输出重复的最后一遍),
// 注意上面的时候已经排好序了 ,所以才能使用这样
cout << arr[i] << " " << count[arr[i]+MAX/2] << endl;
}
delete[]arr;//记得销毁,良好习惯
delete[]count;
return 0;
}
void my_sort(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
int tmp;
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
第三次上机实验:
#include<iostream>
#include<string.h>
using namespace std;
class Point
{
private:
int _x;
int _y;
public:
Point(int x = 0, int y = 0) :_x(x), _y(y) {}//这里是用了初始化列表对坐标进行初始化
//operator的重载
void operator ++()
{
++_x;
++_y;
}
void operator --()
{
--_x;
--_y;
}
void print()
{
cout << "_x=" << _x << endl;
cout << "_y=" << _y << endl;
}
};
class CRole
{
protected:
char *name;
public:
//构造函数
CRole(const char* n)
{
name = new char[strlen(n) + 1];
strcpy(name, n);
}
virtual ~CRole()
{
delete[]name;
}//虚析构函数
virtual void printInfo() = 0;
};
class CPlane : public CRole
{
public:
CPlane(const char*n) :CRole(n) {}//目的是初始化基类成员CPlane的成员变量name CRole(n)是调用了基类构造函数
void printInfo() { cout << "CPlane:" << name << endl; }
};
class CTank :public CRole
{
public:
CTank(const char* n) :CRole(n) {}
void printInfo() { cout << "CTank:" << name << endl;}
};
class CBullet :public CRole
{
public:
CBullet(const char *n) :CRole(n) {}
void printInfo() { cout << "CBullent:" << name << endl; }
};
int main()
{
CRole* objects[15];
objects[0] = new CPlane("Plane 1");
objects[1] = new CPlane("Plane 2");
objects[2] = new CPlane("Plane 3");
objects[3] = new CPlane("Plane 4");
objects[4] = new CPlane("Plane 5");
objects[5] = new CPlane("Plane 6");
objects[6] = new CPlane("Plane 7");
objects[7] = new CPlane("Plane 8");
objects[8] = new CPlane("Plane 9");
objects[9] = new CPlane("Plane 10");
objects[10] = new CPlane("Plane 11");
objects[11] = new CPlane("Plane 12");
objects[12] = new CPlane("Plane 13");
objects[13] = new CPlane("Plane 14");
objects[14] = new CBullet("CBullet 1");
for (int i = 0;i < 15;i++)
{
objects[i]->printInfo();
}
for (int i = 0;i < 15;i++)
{
delete objects[i];
}
return 0;
}