编程算法 - 求1+2+...+n(构造函数) 代码(C++)

求1+2+...+n(构造函数) 代码(C++)

 

本文地址: http://blog.csdn.net/caroline_wendy

 

题目: 求1+2+...+n, 要求不能使用乘除法\for\while\if\else\switch\case等关键字及条件判断语句(A?B:C).

 

可以使用构造函数, 循环求解, 使用数组构造多个类, 使用类的静态变量存储数据.

 

代码:

 

/*
 * main.cpp
 *
 *  Created on: 2014.7.12
 *      Author: spike
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

using namespace std;

class Temp {
public:
	Temp() {++N; Sum+=N;}
	static void Reset() {N=0; Sum=0;}
	static size_t GetNum() {return Sum;}
private:
	static size_t N;
	static size_t Sum;
};

size_t Temp::N = 0;
size_t Temp::Sum = 0;

size_t Sum(size_t n) {
	Temp::Reset();
	Temp* a = new Temp[n];
	delete[] a;
	a = NULL;
	return Temp::GetNum();
}

int main(void)
{
	size_t result = Sum(10);
    printf("result = %d\n", result);
    return 0;
}


输出:

 

 

result = 55

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以给你提供一些代码实现的思路和参考资料。 主存-Cache地址映射是计算机组成原理中的一个重要概念。在实现主存-Cache地址映射时,需要考虑以下几个方面: 1. 地址转换机制:主存-Cache地址映射需要实现地址转换机制,将主存地址转换为Cache地址。常见的地址转换机制包括直接映射、全相联映射和组相联映射。 2. 缓存替换算法:当Cache已满时,需要使用缓存替换算法,将新的数据写入到Cache中。常见的缓存替换算法包括最近最少使用算法(LRU)、先进先出算法(FIFO)和随机替换算法。 3. 缓存写回策略:当Cache中的数据被修改后,需要选择缓存写回策略。常见的缓存写回策略包括写回策略和写直达策略。 以下是一个简单的C++程序,实现了主存-Cache地址映射的基本功能,包括地址转换、缓存替换和缓存写回。缓存大小为4,使用直接映射地址转换机制和最近最少使用算法进行缓存替换,使用写回策略进行缓存写回。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; // 定义Cache块结构体 struct CacheBlock { int tag; bool valid; bool dirty; vector<int> data; int accessTime; }; // 定义Cache结构体 struct Cache { int blockSize; int cacheSize; int blockNum; int tagBits; int indexBits; int offsetBits; vector<CacheBlock> blocks; // 构造函数 Cache(int blockSize, int cacheSize) { this->blockSize = blockSize; this->cacheSize = cacheSize; this->blockNum = cacheSize / blockSize; this->tagBits = 32 - __builtin_clz(blockSize) - __builtin_clz(blockNum); this->indexBits = __builtin_ctz(blockSize); this->offsetBits = 32 - this->tagBits - this->indexBits; this->blocks.resize(blockNum); for (int i = 0; i < blockNum; i++) { blocks[i].tag = -1; blocks[i].valid = false; blocks[i].dirty = false; blocks[i].data.resize(blockSize); blocks[i].accessTime = 0; } } // 地址转换函数 int addressToIndex(int address) { return (address >> offsetBits) & (blockNum - 1); } int addressToTag(int address) { return address >> (offsetBits + indexBits); } // 缓存替换函数 int replaceBlock(int index, int tag) { int replaceIndex = 0; int minAccessTime = INT_MAX; for (int i = index; i < blockNum; i += blockNum) { if (!blocks[i].valid) { replaceIndex = i; break; } if (blocks[i].accessTime < minAccessTime) { minAccessTime = blocks[i].accessTime; replaceIndex = i; } } if (blocks[replaceIndex].dirty) { // 缓存写回 cout << "Cache block " << replaceIndex << " is dirty, write back to memory" << endl; } blocks[replaceIndex].tag = tag; blocks[replaceIndex].valid = true; blocks[replaceIndex].dirty = false; blocks[replaceIndex].accessTime = 0; return replaceIndex; } // 缓存读取函数 int read(int address) { int index = addressToIndex(address); int tag = addressToTag(address); int offset = address & (blockSize - 1); if (blocks[index].valid && blocks[index].tag == tag) { // 命中Cache blocks[index].accessTime++; return blocks[index].data[offset]; } else { // 未命中Cache cout << "Cache miss!" << endl; int replaceIndex = replaceBlock(index, tag); // 从主存中读取数据到Cache cout << "Read data from memory to cache block " << replaceIndex << endl; for (int i = 0; i < blockSize; i++) { blocks[replaceIndex].data[i] = address + i; } blocks[replaceIndex].accessTime++; return blocks[replaceIndex].data[offset]; } } // 缓存写入函数 void write(int address, int value) { int index = addressToIndex(address); int tag = addressToTag(address); int offset = address & (blockSize - 1); if (blocks[index].valid && blocks[index].tag == tag) { // 命中Cache blocks[index].accessTime++; blocks[index].data[offset] = value; blocks[index].dirty = true; } else { // 未命中Cache cout << "Cache miss!" << endl; int replaceIndex = replaceBlock(index, tag); // 从主存中读取数据到Cache cout << "Read data from memory to cache block " << replaceIndex << endl; for (int i = 0; i < blockSize; i++) { blocks[replaceIndex].data[i] = address + i; } blocks[replaceIndex].accessTime++; blocks[replaceIndex].data[offset] = value; blocks[replaceIndex].dirty = true; } } }; int main() { Cache cache(4, 16); cout << "Read data from address 0: " << cache.read(0) << endl; cout << "Write data to address 8" << endl; cache.write(8, 123); cout << "Read data from address 4: " << cache.read(4) << endl; cout << "Write data to address 12" << endl; cache.write(12, 456); cout << "Read data from address 8: " << cache.read(8) << endl; return 0; } ``` 该程序实现了一个大小为16字节的Cache,每个Cache块大小为4字节,使用直接映射地址转换机制和最近最少使用算法进行缓存替换,使用写回策略进行缓存写回。程序输出结果如下: ``` Cache miss! Read data from memory to cache block 0 Read data from address 0: 0 Write data to address 8 Cache miss! Read data from memory to cache block 2 Read data from address 4: 4 Write data to address 12 Cache miss! Read data from memory to cache block 1 Read data from address 8: 123 ``` 希望以上信息能够对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SpikeKing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值