- UID
- 418
- 精华
- 积分
- 3994
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
发表于 2015-9-18 02:20:04
|
显示全部楼层
帮你写好了,没有用到任何数组,除了该死的指针操作使用C的stdlib库,完全面向对象:
算法思想曾经发过链接,实测有效,你来使用一下:
- #include <iostream>
- #include <stdlib.h>
- #define _INP_
- #define _OUP_
- #define _UMR_
- #define DECLARE__
- using namespace std;
- typedef struct
- {
- int source;
- int times;
- }HIPPO;
- class Hippocampus
- {
- public:
- int ctr; //索引
- HIPPO* hippo = NULL; //海马体
- DECLARE__ ~Hippocampus( //析构函数
- _UMR_ void
- );
- DECLARE__ void CheckItems( //检查记忆
- _INP_ const int num //要检查的数字
- );
- DECLARE__ void PrintReport( //打印结果
- _UMR_ void //没有参数
- );
- };
- int main()
- {
- Hippocampus *hippocampus = new Hippocampus();
- int i;
- while (cin >> i)
- {
- hippocampus->CheckItems(i);
- //显示结果
- cout << "\t输入完毕,结果如下:" << endl;
- hippocampus->PrintReport();
- }
- //释放资源
- delete hippocampus;
- }
- void Hippocampus::CheckItems(
- _INP_ const int num
- )
- {
- int k, j;
- //海马体空
- if (hippo == NULL)
- {
- //初始化索引
- ctr = 1;
- //申请内存
- hippo = (HIPPO*)malloc(sizeof(HIPPO));
- //添加记忆
- hippo->source = num;
- hippo->times = 1;
- return;
- }
- //遍历,指针已经位于开始
- bool bFinded = true;
- for (k = 0; k < ctr; k++, hippo++)
- {
- if (num == hippo->source) //找到记忆
- {
- //次数+1
- hippo->times++;
- //跳出循环
- break;
- }
- else
- bFinded = false;
- }
- //指针指向开始
- for (j = k; j > 0; j--, hippo--);
- //没有记忆
- if (!bFinded)
- {
- //长度加一
- ctr++;
- hippo = (HIPPO*)realloc(hippo, ctr * sizeof(HIPPO));
- //指针指向末尾
- for (j = 0; j < ctr - 1; j++, hippo++);
- //添加记忆
- hippo->source = num;
- hippo->times = 1;
- //指针恢复开始
- for (j = 0; j < ctr - 1; j++, hippo--);
- }
- return;
- }
- void Hippocampus::PrintReport(
- void
- )
- {
- int k, j;
- //遍历打印
- for (k = 0; k < ctr; k++, hippo++)
- {
- cout << "\t数字::" << hippo->source << ",";
- cout << "\t频率::" << hippo->times << endl;
- }
- //恢复指针指向开始
- for (j = k; j > 0; j--, hippo--);
- }
- Hippocampus::~Hippocampus( //析构函数
- void
- )
- {
- //释放资源
- free(hippo);
- hippo = NULL;
- }
复制代码 |
|